I have a form with multiple checkboxes. When checked, the value will be "yes" when submitted. I am trying to find the best way to assign the value "no" to all unchecked checkboxes when the form is submitted.
I can't seem to get this to work. Here is what I have:
$('#foo :checkbox').submit(function() {
var $this = $(this);
// $this will contain a reference to the checkbox
if ($this.is('not(:checked)')) {
// the checkbox was not checked
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", $(this).attr("name"));
input.setAttribute("value", "no");
//append to form element that you want .
document.getElementById("#foo").appendChild(input);
} else {
}
});
Why isn't this working?
.click(), not.submit().