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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing CheckBox to a method

    hi i have a html page in which a link calls a method and passes the name of list of checkboxes sharing the same name .Now within that method i want to know the no of checkboxes
    i.e something like checkBoxname.length .
    Can someone help me with the code as it is very urgent :-

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're on the right track. When you have a set of checkboxes all with the same name, you get an array of all those checkboxes when you reference that name.

    In JavaScript, since it's an array, it automatically has a "length" attribute. Just use it.

  • #3
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi ,
    actually the problem is that in the method i am passing the name of the set of checkbox in my case the name is variableNameCheck
    and i call the method as :
    javascript:checkAll('variableNameCheck')
    now in the method :
    function checkAll(obj)
    {

    alert(document.forms[0].obj.length);

    }

    it gives me an undefined in the alertbox .
    hope u r getting my problem.pls help urgently

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Does this help?
    <script type="text/javascript">
    function checkAll(obj){
    alert(document.forms[0][obj.name].length);
    }
    </script>
    </HEAD>

    <BODY>
    <form>
    <input type="checkbox" name="chk" onclick="checkAll(this)">Check 1<br>
    <input type="checkbox" name="chk" onclick="checkAll(this)">Check 2<br>
    <input type="checkbox" name="chk" onclick="checkAll(this)">Check 3<br>
    <input type="checkbox" name="chk" onclick="checkAll(this)">Check 4<br>
    <input type="checkbox" name="chk_2" onclick="checkAll(this)">Check A<br>
    <input type="checkbox" name="chk_2" onclick="checkAll(this)">Check B<br>
    <input type="checkbox" name="chk_2" onclick="checkAll(this)">Check C<br>

    </form>


    .....Willy

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Code:
    function checkAll(chkName)
    {
        alert(document.forms[0].elements[chkName].length);//test
        var objChks = document.forms[0].elements[chkName];
        for (var j=0;j<objChks.length;i++){
             objChks[i].checked = true;
        }
    }
    If the checkbox is dynamically generated by a server-side script and there is a chance that only one checkbox (or none at all) will exist, the code should be like this:

    Code:
    function checkAll(chkName)
    {
        var objChks = document.forms[0].elements[chkName];
        if (objChks) {
           if (typeof objChks.length != 'undefined'){ //multiple checkboxes
              for (var j=0;j<objChks.length;i++){
                   objChks[i].checked = true;
              }
           }
           else objChks.checked = true; //only one checkbox
        }
    }

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Parijat
    hi ,
    actually the problem is that in the method i am passing the name of the set of checkbox in my case the name is variableNameCheck
    and i call the method as :
    javascript:checkAll('variableNameCheck')
    If nothing else, you're passing a string with the value "variableNameCheck", you are not passing the thing/object variableNameCheck. Take out the quotes.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Parijat is passing the name of the checkboxes not the checbox object.
    hi ,
    actually the problem is that in the method i am passing the name of the set of checkbox in my case the name is variableNameCheck
    and i call the method as :
    java script:checkAll('variableNameCheck')
    now in the method :
    function checkAll(obj)
    {

    alert(document.forms[0].obj.length);

    }

    it gives me an undefined in the alertbox .
    hope u r getting my problem.pls help urgently
    Parijat just used a misleading parameter name obj but he/she meant checkbox name. And I correct it in the code I posted above.

    But if variableNameCheck is actually a variable that holds the name of the checkboxes then there should be indeed no quotes when you pass it in checkAll() function. But if the name of the checkboxes is literally variableNameCheck, then the quotes are required.


  •  

    Posting Permissions

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