Page 1 of 1

Collections of IDs

Posted: Fri Oct 01, 2004 11:24 am
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

Posted: Fri Oct 01, 2004 11:48 am
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;

Posted: Fri Oct 01, 2004 1:11 pm
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

Posted: Fri Oct 01, 2004 1:48 pm
by feyd
  1. srcObj doesn't exist.
  2. you aren't passing in a form object. You're passing a CheckBox object.