title: Create hidden input field with script
body: <p>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.</p>
  
  <p>I can't seem to get this to work. Here is what I have:</p>
  
  <pre><code>$('#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 {
  
    }
  });
  </code></pre>
  
  <p>Why isn't this working?</p>