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
    Jul 2011
    Posts
    40
    Thanks
    16
    Thanked 0 Times in 0 Posts

    radio button and javascript to make action

    The purpose is to figure out which one of the radio buttons is checked. What is below in the IF statement works if there is no <label> used in the html part. In this case <label> is used so how should the IF statement be modified?

    Code:
    function set12h24() {
        if (document.switch12h24_form.radio12h24.value == "24h")
        {
            do something here.
        }
    }
    
    <div id="radio_side">
        <form name="switch12h24_form">
            <label class="h2412off">&nbsp;<input type="radio" name="radio12h24" value="12h" class="h2412_button" ><a onclick="set12h24()">12h</a></label>
            <label class="h2412off">&nbsp;<input type="radio" name="radio12h24" value="24h" class="h2412_button" ><a onclick="set12h24()">24h</a></label>
        </form>
    </div>
    Thanks!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,894
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    You are trying to make your radio buttons into links. There seems to be no purpose in that.


    Code:
    <script type = "text/javascript">
    function set12h24() {
    var r = document.switch12h24_form.rad12h24;
    for (var i=0; i<r.length; i++) {
    if (r[i].checked) {
    var val = r[i].value;
    }
    }
    if (val == "24h") {
    alert (val);
    // do something here
    }
     
    }
    </script>
    
    <div id="radio_side">
        <form name="switch12h24_form">
            <label class="h2412off">&nbsp;<input type="radio" name="rad12h24" value="12h" class="h2412_button" onclick="set12h24()">12h</label>
            <label class="h2412off">&nbsp;<input type="radio" name="rad12h24" value="24h" class="h2412_button" onclick="set12h24()">24h</label>
        </form>
    </div>
    Quizmaster: Castel Gandolfo is the summer residence of which religious leader?
    Contestant: Jesus

    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.

  • Users who have thanked Philip M for this post:

    JonesJ (01-20-2013)


  •  

    Tags for this Thread

    Posting Permissions

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