Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check All Checkbox

    I found a free script on checking all boxes with one click of select all checkbox. But i don't know how to alter it to fit my script.

    j=0
    <form name="frmOrders" action="pass.asp" method="post">
    while not objrs.eof
    <input type="checkbox" name="<%=j%>.stockitem" value="<%=objrs("ID")%>"><br>
    objrs.movenext
    wend
    <br>
    <input type=checkbox name="selectall" value="" onClick="selectAll()">
    </form>

    <script language="Javascript">
    function selectAll() {
    if (document.frmOrders.countitems.value=="0") {} else
    if (document.frmOrders.countitems.value=="1") {
    document.frmOrders.stockitem.checked=document.frmOrders.selectall.checked;
    } else {
    for (var x=0; x<document.frmOrders.countitems.value; x++) {
    document.frmOrders.items[x].checked=document.frmOrders.selectall.checked;
    }
    }
    }
    </script>

    stockitem.checked is not right in my javascript. how can i include the value of <%=j%>.stockitemi in the javascript?

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    dont include that in your script, loop through all form elements and if the elementtype is CHECKBOX, then check it

    like:

    var elem = document.forms["frmOrders"].elements;
    for (i in elem) {
    if (i.type == "checkbox" && (i.name.indexof("stockitem") > -1)){
    i.checked = true;
    }
    }

    this is not tested code, but it should give you an idea how to handle it
    I am the luckiest man in the world

  • #3
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Check All Checkbox

    Originally posted by carrera
    I found a free script on checking all boxes with one click of select all checkbox. But i don't know how to alter it to fit my script.
    A trick I use relies on declaring the checkboxes as an array of checkbox objects (just give them all the same name, just like radio buttons). Here's a sample...
    Code:
    <form action="">
    	<input name="scope" type="radio" onclick="setAll(this,true)">All
    	<input name="scope" type="radio" onclick="setSome(this)">Some
    	<input checked name="scope" type="radio" onclick="setAll(this,false)">None<br>
    	<br>
    	<input name="choice" type="checkbox" onclick="setSome(this)">Bees<br>
    	<input name="choice" type="checkbox" onclick="setSome(this)">Money<br>
    	<input name="choice" type="checkbox" onclick="setSome(this)">Psychology<br>
    	<input name="choice" type="checkbox" onclick="setSome(this)">Web Development<br>
    </form>
    <script type="text/javascript" language="javascript">
    function setSome(chk)
    {
    	var sum=0;
    	with(chk.form)
    	{
    		for(var i=0;i<choice.length;i++)sum+=(choice[i].checked?1:0);
    		scope[0].checked=(sum==choice.length);
    		scope[2].checked=(sum==0);
    		scope[1].checked=(!scope[0].checked && !scope[2].checked);
    	};
    };
    function setAll(chk,state)
    {
    	if(chk.checked)with(chk.form)for(var i=0;i<choice.length;i++)choice[i].checked=state;
    	setSome(chk);
    };
    </script>
    Hopefully, you can adapt this script for your own application.

    :ca_redwards.
    Attached Files Attached Files


  •  

    Posting Permissions

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