Skip to content Skip to sidebar Skip to footer

Javascript Changing Checkbox Status Before Submitting Form

EDIT Note on the user experience... After the first comment I'd like to point out that every submit leads to a POST/redirect/GET and take the user to a new page. It's actually a

Solution 1:

@Cedric...my solution may not work on IE ( 8 & 9) ....other than that it works just fine. For the issue on IE 8 and IE 9 I raised this If for some reasons it worked on IE 8/9 please let me know :)

<inputtype="checkbox" name="collapseFolds" value="na" checked onchange="document.getElementById('someid').submit()">

Instead of writing inline onchange you probably want to keep html and javascript separate for ease of future maintenance.

so you can change your code to:

HTML:

<inputtype="checkbox" name="collapseFolds" value="na" checked >

Javascript:

functionsubmitThisFormOnCheckBoxClick(event){
    event.preventDefault(); // This will ensure that history will be taken care ofvar form = document.getElementById("formId");
    form.submit();
}

var collapsefolds = document.getElementsByTagName("collapseFolds");

for (var el in collapsefolds){

   if (el.addEventListener) { // for non-IE browsers
    el.addEventListener('change', modifyText, false); 
   } elseif (el.attachEvent)  { // for IE based browsers
    el.attachEvent('change', modifyText);
   }
}

Let me know if you have any questions.

Post a Comment for "Javascript Changing Checkbox Status Before Submitting Form"