I have multiple forms in a page that contains a number of input checkboxes, when one of the form inputs change I want to collect all the parent forms data into a JSON array so I can post this somewhere.
I am struggling to get the post data into an array, I was planning to use this.closest("form").serializeObject() but this doesn't appear to work.
I get the following error:
Uncaught TypeError: this.closest(...).serializeObject is not a function
$(document).ready(function() {
console.log("jquery ready!");
$('form input:checkbox').change(function() {
var jsonArray;
var formData = this.closest("form").serializeObject();
console.log(formData);
});
});
<form action="http://myurl.com/1" id="form-1">
<label for=""><input type="checkbox" name="monday" value="1" id=""> Monday</label>
<label for=""><input type="checkbox" name="tuesday" value="1" id=""> Tuesday</label>
</form>
<form action="http://myurl.com/2" id="form-2">
<label for=""><input type="checkbox" name="monday" value="1" id=""> Monday</label>
<label for=""><input type="checkbox" name="tuesday" value="1" id=""> Tuesday</label>
</form>
<form action="http://myurl.com/3" id="form-3">
<label for=""><input type="checkbox" name="monday" value="1" id=""> Monday</label>
<label for=""><input type="checkbox" name="tuesday" value="1" id=""> Tuesday</label>
</form>