HTML Tutorial


 Forum HomeForum Home   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
How to check all the Checkbox...
Post new topic   Reply to topic    HTML Help Forum Index -> HTML Form
View previous topic :: View next topic  
Author Message
cemplukcute2003



Joined: 27 Dec 2004
Posts: 28

PostPosted: Thu Jan 06, 2005 11:47 pm     How to check all the Checkbox... Reply with quote

Hi, i've problem here.

I want to make a page that when i press the button CheckAll, all the checkbox will be checked (like Yahoo). i've made function using javascript but it didn't work. Here is my code. Help me please...

function checkAll() {
var checked = false;

for(var i=1;;i++) {
tt = document.FormDel['chkbox[' + i + ']'];
alert(tt);
if(tt != null) {
if (tt.checked) {
checked = true;
}
}
else
break;
}
}

<TD> <INPUT TYPE=checkbox NAME='chkbox[]'> </TD>
<INPUT TYPE=Button NAME='CheckAll' VALUE='Check All' onClick='checkAll(); return true;'>
kanenas



Joined: 14 Dec 2004
Posts: 191

PostPosted: Fri Jan 07, 2005 5:05 am     Re: How to check all the Checkbox... Reply with quote

cemplukcute2003 wrote:
Code:
function checkAll() {
  var checked = false;
  for(var i=1;;i++) {


'i' should be initialized to 0, not 1.

cemplukcute2003 wrote:
Code:
  for(var i=1;;i++) {
    tt = document.FormDel['chkbox[' + i + ']'];



Unless you've defined document.FormDel somewhere and set various 'chkbox['+i+']' properties, this statement has two errors. Furthermore, the naming of the checkbox looks like the PHP naming idiom for form fields. This doesn't cause a problem, but it won't do anything for javascript.

cemplukcute2003 wrote:
Code:
    alert(tt);
    if(tt != null) {
      if (tt.checked) {
        checked = true;
      }
    } else break;
  }
}


'checked = true' won't affect anything as the function is currently written; you probably want to replace it with 'tt.checked = true' and strip the surrounding 'if'.

Once you resolve on the above issues, your function should work. Use of 'break' to exit the loop is odd, but not egregiously wrong and is a little hackish. It could miss checkboxes if whatever array you've built to hold the checkbox references has any null elements, but I doubt that would happen. Even so, it might be better to use the 'for' loop as J.W. Backus et. al intended and have a loop termination test in the 'for' statement rather than the 'break;'. 'var checked' isn't necessary, but also isn't harmful.

All that being said, this is how I would do it.
Code:

<form id='Meriwether'>
<input type='checkbox' id='chk0' name='c0'>0</input> <br>
<input type='checkbox' id='chk1' name='c1'>1</input> <br>
<input type='checkbox' id='chk2' name='c2'>2</input> <br>
<button id='check' onclick='checkAll(this.form)'>check all</button>
<button id='uncheck' onclick='uncheckAll(this.form)'>uncheck all</button>
</form>

<script type='text/javascript'><!--
function getElementsByTagName(node, tagName) {
  tagName=tagName.toUpperCase();
  var els = [];
  if (node.elements) {
    node = node.elements;
  }
  for (i in node) {
    if (node[i] && tagName == node[i].tagName) {
      els.push(node[i]);
      if (node[i].id) {
        els[node[i].id] = node[i];
      }
      /* Could also record nodes by name in els, but it's probably not worth the
         computation
      */
    }
    /*should probably recurse; if so, test whether sub-nodes have already
      been added to els.
      */

  }
  return els;
}

function setCheckedState (form, state) {
  if (!form) {
    form = document;
  }
  if (!form.getElementsByTagName) {
    form.getElementsByTagName = function(tagName) {
      return getElementsByTagName(this, tagName);
    }
  }

  var inputs = form.getElementsByTagName('input');
  for (i=0; i<inputs.length; ++i) {
    if ('checkbox' == inputs[i].type.toLowerCase()) {
      inputs[i].checked=state;
    }
  }
}

function checkAll(form) {
  setCheckedState(form, true);
}

function uncheckAll(form) {
  setCheckedState(form, false);
}

//-->
</script>
cemplukcute2003



Joined: 27 Dec 2004
Posts: 28

PostPosted: Fri Jan 07, 2005 8:01 am     Reply with quote

Hei, u're right. I miss the tt.checked.
thx a lot, it works now
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum Index -> HTML Form All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Webmaster Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial