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
    Mar 2007
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to get the selected checkbox

    Hello,

    I would like to know how to get the selected checkbox, I know how to get the selected radiobox wich the code is listed below:

    this.options[this.selectedIndex].value

    Thanks in advance...

    W. Reagrds

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by snake_eyes View Post
    Hello,

    I would like to know how to get the selected checkbox, I know how to get the selected radiobox wich the code is listed below:

    this.options[this.selectedIndex].value

    Thanks in advance...

    W. Reagrds
    this.options[this.selectedIndex].value
    does not give the value of a radio button - it gives the value of a selected option in an options array.

    You want

    document.formname.checkboxname.value

    BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's assistance for granted.

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello,

    Yes exactly my code is for the options, by mistake I sent it as radio box, however Philip M your code is for checkbox but what about if I have the checkbox name such as myname[] how the code should be?

    and thanks for your note, but I said thanks in advance I mean Any inputs with this post is highly appreciated

    W. Regards

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    I hope this example helps you:-

    <form name = "myform">
    <input name = "myname1" type = "checkbox" value = "Red"> RED <br>
    <input name = "myname2" type = "checkbox" value = "Blue"> BLUE <br>
    <input name = "myname3" type = "checkbox" value = "Green"> GREEN <br>
    <input name = "myname4" type = "checkbox" value = "Yellow"> YELLOW<br><br>
    <input type = "button" value = "Click Me" onclick="cboxlist()"><br>
    </form>

    <script type = "text/javascript">

    function cboxlist(){
    var choice = "Your colour choice is: ";
    var x = "";
    for (var i=1; i<5; i++) {
    x = eval("document.myform.myname" + i + ".value");
    y = eval("document.myform.myname" + i + ".checked");
    if (eval("y")) {
    choice = choice + " " + x;
    }
    }
    if (choice.length <= 24) {
    choice = choice + "NONE";
    }
    alert (choice);
    }

    </script>


  •  

    Posting Permissions

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