Question:

JavaScript Help!!! Check Boxes - Select all Function?

by  |  earlier

0 LIKES UnLike

i have the following code:

<form name="frmListCategory">

<table>

<tr><td><input type="checkbox" name="frmCategory[]" id="frmCategory" value="4"></td>

<td> Cookies</td></tr>

<tr><td><input type="checkbox" name="frmCategory[]" id="frmCategory" value="6"></td>

<td> Electronics</td></tr>

</table>

</form>

I also have java script function

function selectall(chkbox) {

for (i=0; i < chkbox.length; i++)

chkbox[i].checked = true ;

}

function unselectall(chkbox) {

count=0;

for (i=0; i < chkbox.length; i++){

if(chkbox[i].checked)

count++;

chkbox[i].checked = false ;

}

if(count==0){

alert("Select something to Unselect");

}

}

If i call these two functions its workin perfectly!!

selectall(document.frmListCategory.frm...

Suppose if there is only one row it is not working.

These rows were generated at server side.

if only one row is generated, then it is not working.

For all more than one its working

Someone Help! Please

 Tags:

   Report

3 ANSWERS


  1. try selectall(document.frmListCategory);


  2. mainframe computing-http://gotogol.info/

  3. Interesting. Javascript doesn&#039;t recognize it as an array if there&#039;s only one element. A very unsexy solution could be to include an invisible checkbox if at least one checkbox is to be returned. So there would always be at least 2 boxes as far as JS is concerned. Just remember to always ignore the first box (where value == 0) when the form is submitted.

    &lt;form name=&quot;frmListCategory&quot;&gt;

    &lt;span style=&quot;display:none&quot;&gt;&lt;input type=&quot;checkbox&quot; name=&quot;frmCategory[]&quot; id=&quot;frmCategory&quot; value=&quot;0&quot;&gt;

    &lt;/span&gt;

    &lt;table&gt; ...

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions