SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot af11kcc's Avatar
    Join Date
    Oct 2003
    Location
    South of Rio Grande
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    limiting number of checked checkboxes

    I have an app where I show 40 checkboxes but I need to limit the amount of "checked" checkboxes to 10....
    Anybody can help with this, I have been trying some approaches but I am not getting any closer...
    Thanks

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put onclick="checkThis(this, form)" in each checkbox:

    Code:
    function checkThis(obj, form)
    {
       var count = 0;
       for (var i = 0; i < form.elements.length(); i++)
       {
          if (form.elements[i].type == "checkbox")
          {
             count++;
          }
       }
       if (count > 10)
       {
          obj.checked = false;
       }
    }
    ck :: bringing chris to the masses.

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">
    
    function limitchex(oCheckbox, limit) 
    {
    	var el, i = 0, n = limit, oForm = oCheckbox.form;
    	while (el = oForm.elements[i++])
    	{
    		if (el.className == 'limited' && el.checked)
    			--n;
    		if (n < 0)
    		{
    			alert('Please select no more than ' + limit + ' checkboxes.')
    			return false;
    		}
    	}
    	return true;
    }
    
    </script>
    </head>
    <body>
    <form>
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 1<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 2<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 3<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 4<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 5<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 6<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 7<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 8<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 9<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 10<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 11<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 12<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 13<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 14<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 15<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 16<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 17<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 18<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 19<br />
    <input type="checkbox" class="limited" onclick="return limitchex(this,10)" /> 20<br /><br />
    <input type="reset" />
    </form>
    </body>
    </html>
    ::: certified wild guess :::

  4. #4
    SitePoint Zealot af11kcc's Avatar
    Join Date
    Oct 2003
    Location
    South of Rio Grande
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it works great


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
  •