SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast roloenusa's Avatar
    Join Date
    Mar 2004
    Location
    Moorpark
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Counting the number of checked checkboxexs

    Hey guys.. I know you can help me with this one.

    I have page that displays de all the users in a database. Next to each name there is a check box so the administrator can check each user.

    What i need is a function that counts (live), how many boxes have been check. all the check boxes have the same name Name=username but different values.

    Do you know how to do something like that.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function areChecked(grpname)
    {
    	var oForm = document.forms[0]; //1st form in document (counts from zero)
    	var box, i = howmany = 0;
    	while (box = oForm.elements[grpname][i++])
    		if (box.checked)
    			++howmany;
    	return howmany;
    }
    Sample call: var num_checked = areChecked('username');
    ::: certified wild guess :::

  3. #3
    SitePoint Enthusiast roloenusa's Avatar
    Join Date
    Mar 2004
    Location
    Moorpark
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi adios!

    Thanks for your help.. But i can't get it to work. I'm not good at javascript....

    where do i enter the form name and the checkbox set name? and how do i out put the result?

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, on my way out. hth:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <script type="text/javascript">
    
    function areChecked(grpname)
    {
    	var oForm = document.forms[0]; //1st form in document (counts from zero)
    	var box, i = howmany = 0;
    	while (box = oForm.elements[grpname][i++])
    		if (box.checked)
    			++howmany;
    	return howmany;
    }
    
    </script>
    </head>
    <body>
    <form>
    <input type="checkbox" name="username" value="" /> user 1<br />
    <input type="checkbox" name="username" value="" /> user 2<br />
    <input type="checkbox" name="username" value="" /> user 3<br />
    <input type="checkbox" name="username" value="" /> user 4<br />
    <input type="checkbox" name="username" value="" /> user 5<br /><br />
    <input type="text" name="num_of_users" value="" size="2" />
    <input type="button" value="# of Users?" onclick="num_of_users.value=areChecked('username')" />
    </form>
    </body>
    </html>
    Could also use document.forms['form_name']
    ::: certified wild guess :::


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •