Possible for javascript to handle 2 values from a radio button?

Discussion in 'Javascript' started by lee@dnuk.com, Jul 29, 2005.

  1. Guest

    Hello

    I need some javascript code I've written to handle 2 values from an
    HTML form.

    I've been successful using the "label" attribute (I've simplied the
    example below):

    <input type="radio" name="group1" value="3" label="Cheese"
    checked>Cheese<br>
    <input type="radio" name="group1" value="5" label="Meat">Meat<br>
    <input type="radio" name="group1" value="15" label="Bread">Bread<br>

    But this only works in Internet Explorer, it will not work in
    Mozilla/Firebox.

    I've also tried id="" but that does not work.

    Does anyone know how I can refence 2 values per radio button in
    javascript?
    , Jul 29, 2005
    #1
    1. Advertising

  2. wrote:


    > I've been successful using the "label" attribute (I've simplied the
    > example below):
    >
    > <input type="radio" name="group1" value="3" label="Cheese"
    > checked>Cheese<br>
    > <input type="radio" name="group1" value="5" label="Meat">Meat<br>
    > <input type="radio" name="group1" value="15" label="Bread">Bread<br>
    >
    > But this only works in Internet Explorer, it will not work in
    > Mozilla/Firebox.


    I don't see any script in that example so if anything does not work or
    does not work as you want it why do you ask in a JavaScript group?


    > Does anyone know how I can refence 2 values per radio button in
    > javascript?


    I am not sure I understand what you are looking for. Do you want to read
    out the value of the label attribute of an element? Then use
    element.getAttribute('label')
    so assuming you have your radio button group inside of a
    <form name="formName">
    then you can access the first radio button as
    var radioButton = document.forms.formName.elements.group[0];
    if (radioButton && radioButton.getAttribute) {
    alert(radioButton.getAttribute('label'));
    }

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jul 29, 2005
    #2
    1. Advertising

  3. Lee Guest

    said:

    >Does anyone know how I can refence 2 values per radio button in
    >javascript?


    <html>
    <head>
    <title>Two Values</title>
    <script type="text/javascript">
    function showSel(f) {
    for (var i=0;i<f.elements.length;i++) {
    if(f.elements.type=="radio" && f.elements.checked) {
    var v=f.elements.value.split(',');
    alert("\""+v[0]+"\" and \""+v[1]+"\"");
    }
    }
    }
    </script>
    </head>
    <body>
    <form>
    <input type="radio" name="group1" value="3,Cheese" checked>Cheese<br>
    <input type="radio" name="group1" value="5,Meat">Meat<br>
    <input type="radio" name="group1" value="15,Bread">Bread<br>
    <input type="button" value="Show Selection" onclick="showSel(this.form)">
    </form>
    </body>
    </html>
    Lee, Jul 29, 2005
    #3
  4. Guest

    Thanks Lee, that's exactly what I needed.

    I've left the script off to keep the question simple.

    Thanks.
    , Jul 29, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hazzard
    Replies:
    4
    Views:
    3,260
    Hazzard
    Jul 9, 2003
  2. Karthick Kumar
    Replies:
    1
    Views:
    784
  3. Scott D Johnson
    Replies:
    1
    Views:
    1,050
    Scott D Johnson
    Sep 27, 2006
  4. Brian Cryer
    Replies:
    2
    Views:
    4,474
    Andy Dingley
    Mar 26, 2007
  5. Hiten
    Replies:
    1
    Views:
    612
    Vidar Petursson
    May 26, 2004
Loading...

Share This Page