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
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    alert values of checked checkbox values

    I have a bunch of checkboxes like below that the user can check some or all and click the button and see the values of all the selected checkboxes. How can I do that?

    Code:
    <script>
    function alertValues(){
    
    
    }
    </script>
    
    
    <input type="checkbox"  class ="normal2" value="131971" name="list[]" >
    <input type="checkbox"  class ="normal2" value="131973" name="list[]" >
    <input type="checkbox"  class ="normal2" value="131975" name="list[]" >
    <input type="checkbox"  class ="normal2" value="131977" name="list[]" >
    
    <input type="button" onClick="alertValues()"

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    Code:
    <script>
    function alertValues(form)
    {
        var cbs = form["list[]"];
        var vals = ""
        for ( var c = 0; c < cbs.length; ++c )
        {
            var cb = cbs[c];
            if ( cb.checked ) vals += "," + cb.value;
        }
        alert( "The values are: " + vals.substring(1) );    
    
    }
    </script>
    ...
    <form><!-- DO NOT FORGET THIS! -->
    <input type="checkbox"  class ="normal2" value="131971" name="list[]" >
    <input type="checkbox"  class ="normal2" value="131973" name="list[]" >
    <input type="checkbox"  class ="normal2" value="131975" name="list[]" >
    <input type="checkbox"  class ="normal2" value="131977" name="list[]" >
    
    <input type="button" onClick="alertValues(this.form)"
    </form>
    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
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts
    I get length in null or not an option error???

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    You are missing the > on the end of the <input type="button"

    When I answered you, I copy/pasted your mistake.
    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
    •