Collections of IDs

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Collections of IDs

Post by anjanesh »

I have this :

Code: Select all

<INPUT TYPE=checkbox ID=SerialAll onClick=CheckUnCheckAll()>
while (...)
 &#123;
 $row=mysql_fetch_assoc(...);
 echo '<INPUT TYPE=checkbox ID='.$row&#1111;'Serial'].'>';
 &#125;
Now in my JS Code I want a function which check/uncheck all other checkboxes under it.
Obviously its something like : document.getElementById("Serial1").checked=true;
But Serial1 is not known actually. I want to know what are the numbers - like Serial23, Serial65, Serial 11 etc - its not in any order.
getElementByNames will give the collection which can be evaluated but is there any way to do it using getElementById ?
Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

untested

Code: Select all

function CheckUnCheckAll(formObj,checkBoxName,initialStateVarName)
&#123;
  if(!formObj || !checkBoxName || !initialStateVarName)
  &#123;
    alert('missing arguments to CheckUnCheckAll()');
    return;
  &#125;
  var box = formObj.elements&#1111; checkBoxName ];
  if(!box)
  &#123;
    alert('invalid checkbox name');
    return;
  &#125;
  var state = eval(initialStateVarName + ' = !' + initialStateVarName);
  for(var x = 0, y = box.length; x < y; x++)
    box&#1111; x ].checked = state;
&#125;
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

The HTML part goes something like this :
<INPUT TYPE=checkbox ID=SerialAll onClick="CheckUnCheckAll(this)">
<INPUT TYPE=checkbox ID=Serial5>
<INPUT TYPE=checkbox ID=Serial9>
<INPUT TYPE=checkbox ID=Serial7>
<INPUT TYPE=checkbox ID=Serial12>
<INPUT TYPE=checkbox ID=Serial15>
<INPUT TYPE=checkbox ID=Serial19>
<INPUT TYPE=checkbox ID=Serial35>
<INPUT TYPE=checkbox ID=Serial40>
<INPUT TYPE=checkbox ID=Serial66>
<INPUT TYPE=checkbox ID=Serial82>

In CheckUnCheckAll()
Serial5 to Serial82 must be checked
{
var box = frmCodes.elements[srcObj];
for(var x = 0, y = box.length; x < y; x++)
box[ x ].checked = true;
}

IE returns : length is null or not an object
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. srcObj doesn't exist.
  2. you aren't passing in a form object. You're passing a CheckBox object.
Post Reply