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 4 of 4
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    20
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Getting checkbox value

    I have a form with a few checkboxes


    Code:
    <input type='checkbox' name='idName[1]' checked><b> Data Cabinet - 7ft - 0113</td></tr>
    <input type='checkbox' name='idName[2]' checked><b> Data Cabinet - 5ft -0463</td></tr>
    <input type='checkbox' name='idName[3]' ><b> Data Cabinet - 5ft - 0463</td></tr>
    I have a string with numbers of the name.
    For instance the string "1,3"
    Where I am trying to get the value of idName[1] and idName[3]

    Code:
    newTempArray =  s1.split(",");
    
    for(var i=0; i<newTempArray.length; i++){
        k = newTempArray[i] ;
        m =  'idName[' + k + ']';
        tempElement = document.getElementsByTagName(m);
        document.write(tempElement);
        document.write(tempElement.getAttribute('checkbox') );
    }
    However I am getting an error on getting the elements value.
    How can I get the elements checked status?

    thanks

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,015
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    getElementsByTagName means the name of the HTML tag. e.g, <a>, <br>, <span>. So you'd use getElementsByTagName("span") for example.

    Code:
    newTempArray =  s1.split(",");
    
    var form = document.forms[0]; // or any other way to get to the <form> containing the checkboxes
    
    for(var i=0; i<newTempArray.length; i++){
        k = newTempArray[i] ;
        var m =  'idName[' + k + ']';
        var cb = form[m];
        var cbvalue = cb.value;
        // but now you CAN NOT use document.write ... so what will you do?
    }
    If you use document.write once a page is complete, it WIPES OUT all content on the page, including even the javascript code that did the document.write!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,015
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Generally, it's a bad idea to name the checkboxes as you did.

    If you using PHP for your server-side code, you are better off using
    Code:
    <input type='checkbox' name='idName[]' checked><b> Data Cabinet - 7ft - 0113</td></tr>
    <input type='checkbox' name='idName[]' checked><b> Data Cabinet - 5ft -0463</td></tr>
    <input type='checkbox' name='idName[]' ><b> Data Cabinet - 5ft - 0463</td></tr>
    If you are not using PHP, then better would be maybe
    Code:
    <input type='checkbox' name='idName_1' checked><b> Data Cabinet - 7ft - 0113</td></tr>
    <input type='checkbox' name='idName_2' checked><b> Data Cabinet - 5ft -0463</td></tr>
    <input type='checkbox' name='idName_3' ><b> Data Cabinet - 5ft - 0463</td></tr>
    But there are exceptions, if you have a really good reason for the naming.

    Maybe the best way, if using PHP, would be:
    Code:
    <input type='checkbox' id="cb1" name='idName[]' checked><b> Data Cabinet - 7ft - 0113</td></tr>
    <input type='checkbox' id="cb1" name='idName[]' checked><b> Data Cabinet - 5ft -0463</td></tr>
    <input type='checkbox' id="cb1" name='idName[]' ><b> Data Cabinet - 5ft - 0463</td></tr>
    And then the JS code is simpler, too:
    Code:
    newTempArray =  s1.split(",");
    
    for(var i=0; i<newTempArray.length; i++){
        var k = newTempArray[i] ;
        var cb = document.getElementById("cb" + k);
        var cbvalue = cb.value;
        // but now you CAN NOT use document.write ... so what will you do?
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,015
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    LOL!!! I JUST NOTICED! You want to get the VALUES of those checkboxes...BUT NOT ONE OF THEM *HAS* a value!!!

    Checkboxes *ONLY* have values if you assign them:
    Code:
    <input type="checkbox" name="whatever" value="THE VALUE GOES HERE"/>
    They do have default values if you don't supply them, but the default is (I think) just "On", so is totally not useful.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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