Web Analytics Made Easy -
StatCounter Passing CheckBox to a method - CodingForum

Announcement

Collapse
No announcement yet.

Passing CheckBox to a method

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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.

    Comment


    • #3
      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

      Comment


      • #4
        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

        Comment


        • #5
          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
              }
          }
          Glenn
          vBulletin Mods That Rock!

          Comment


          • #6
            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.

            Comment


            • #7
              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.
              Glenn
              vBulletin Mods That Rock!

              Comment

              Working...
              X