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 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    JavaScript and check boxes

    How would i get javascript to alert a message, depending on what checkboxes are checked when the user clicks submit.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Code:
    <form name = "myform">
    <input name = "myname" type = "checkbox" value = "Red"> RED <br>
    <input name = "myname" type = "checkbox" value = "Blue"> BLUE <br>
    <input name = "myname" type = "checkbox" value = "Green"> GREEN <br>
    <input name = "myname" type = "checkbox" value = "Yellow"> YELLOW<br><br>
    <input type = "button" value = "Get Color Choices" onclick="cboxlist()"><br>
    </form>
    
    <script type = "text/javascript">
    
    function cboxlist(){
    var choice = "Your colour choice is: ";
    var x = new Array();
    var colors = document.myform.myname;
    for ( i = 0; i < colors.length; i++ ) {
    if ( colors[i].checked ) {
    x.push(colors[i].value);
    }
    }
     alert (choice + (x.length == 0 ? 'NONE' : x.join(' | ')));
    } 
    
    </script>

    "The louder he talked of his honour, the faster we counted our spoons." - Samuel Johnson

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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