multiple checkboxes with same id


Handling multiple checkboxes with same id's


If suppose you have defined more than one checkboxes with sdame id's, then it will treat it as an array of names.

<form name="CheckBoxForm">


<input type="Checkbox" name="myCheckbox" id="myCheckbox"  />
<input type="Checkbox" name="myCheckbox" id="myCheckbox"  />
<input type="Checkbox" name="myCheckbox" id="myCheckbox"  />
<input type="Checkbox" name="myCheckbox" id="myCheckbox"  />
<input type="" onclick="testCheckboxes()" />
</form>


Now according to HTML, id must be unique. So here it will treat them like an array. So if you want to access them, do as
given here,


function testCheckboxes(){
var myCheckboxArray= new Array();
var total=document.getElementsByName('myCheckbox').length;
for(var i=0;i<total;i++){
 alert(document.getElementsByName('myCheckbox')[i].checked);
 if(document.getElementsByName('checkbox')[i].checked == true){
 myCheckboxArray[myCheckboxArray.length]="Checked";
 }
    }

}

Comments

  1. Ashok kumar Knowserve Student java BatchDecember 20, 2011 at 5:34 AM

    If you create same name for checkbox it create array already no need to give id same because id always are unique it also needed if we want particular checkbox we can access it by getElementById

    ReplyDelete

Post a Comment

Popular posts from this blog

Struts 2 - struts tag with boolean property

Android: where to run keytool command in android