javascript - Select at least one option in HTML -


situation: want create simple code whereby users must select @ least 1 extension proceed. users should select @ least 1 or more extension, else alert message appear.

problem: problem is, if there shall 1 extension available selection, whether selected or not, alert message appear disallowing registration complete.

//select atleast 1 extension   var arrcheckboxes = document.checkform.elements["product"];     var checkcount = 0;     (var = 0; < arrcheckboxes.length; i++) {         checkcount += (arrcheckboxes[i].checked) ? 1 : 0;     }     if (checkcount > 0){         return true;     } else {         alert("select @ least 1 extension.");         return false;     } 

it legacy days of browsers if there 1 form control name of product, then:

document.checkform.elements["product"];  

will return reference control, not collection seem expect. such controls not have length property default so:

arrcheckboxes.length 

returns undefined ,

i < arrcheckboxes.length 

is false loop never entered.

to fix that, use queryselectorall returns collection:

var arrcheckboxes = document.checkform.queryselectorall('[name=product]'); 

supported in ie 8+ , everywhere else. simpler version of code (assuming it's in body of function):

var arrcheckboxes = document.checkform.queryselectorall('[name=product]');  (var = 0; < arrcheckboxes.length; i++) {     if (arrcheckboxes[i].checked) return true; } alert("select @ least 1 extension."); return false; 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -