1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Radio Button / getEmenetByID

Discussion in 'JavaScript' started by redhits, Jun 14, 2008.

  1. #1
    Let's say i am having 2 radio buttons with the same and the same ID , how i can know with javascript which one it's checked?


    like


    <input type=radio name=level id=level value=1>
    <input type=radio name=level id=level value=2>

    is there any way i can

    alert(document.getElementById('level').value); ?


    It's seems is not working , is only working with the TagName ...


    How ? this 2 seems to not work

    document.getElementByName('level').value;
    document.getElementByTagName('level').value;



    This seems to not work to :


    if(document.getElementById.('level').[0].checked){alert('1');}
    if(document.getElementById.('level').[1].checked){alert('2');}
     
    redhits, Jun 14, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try document.level.value or document.YourFormName.level.value

    I haven't worked with raw JavaScript in a long time (been using jQuery framework) so I can't be certain.
     
    zerxer, Jun 14, 2008 IP
    redhits likes this.
  3. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #3
    The problem is i think it have no value, because it's a radio button ... i mean a radio button may have many values...

    if you read it with the .value it will input the an value, from all those <input type=radio name=level element...

    even if the element is not checked!
     
    redhits, Jun 14, 2008 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hmm you're right in a way. You'll have to loop them..

    for (i=0;i<document.YourFormName.level.length;i++)
    {
          if (document.YourFormName.level[i].checked)
          {
                 level_value = document.YourFormName.level[i].value;
          }
    } 
    Code (markup):
    If you were using something like jQuery, you could simply do $(":radio[name='level']").val(); :/
     
    zerxer, Jun 14, 2008 IP
  5. aditya_sfs

    aditya_sfs Peon

    Messages:
    2,271
    Likes Received:
    389
    Best Answers:
    0
    Trophy Points:
    0
    #5
    if(document.formname.level[0].checked) {
    ///First radio button is selected
    }
     
    aditya_sfs, Jun 14, 2008 IP
    redhits likes this.
  6. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #6
    No , the problem is that i don't jave a form name for it!
     
    redhits, Jun 14, 2008 IP
  7. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #7
    seems is not working as a main parent , so i had to create a form for it...

    so now with myform.level[x].checked is working...
     
    redhits, Jun 14, 2008 IP
  8. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #8
    Give them both different ids (should never have 2 same ids, its invalid HTML)

    You only need the smae name for radio sets, and you can just use document.getElementById().

    And for pete's sake, put quotes around your attributes, its driving me insane (also invalid HTML).

    <input type=radio name=level id=level value=1>
    Code (markup):
    Should be:

    <input type="radio" name="level" id="level" value="1">
    Code (markup):
     
    blueparukia, Jun 14, 2008 IP
  9. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #9
    You should not have two objects with the same ID, that isnt valid code :\
     
    crath, Jun 14, 2008 IP
  10. koolman

    koolman Peon

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Try below code, you do not need <form>

    level_value=null;
    arrLevels = document.getElementByName("level");
    for (var i=0;i<arrLevels.length;i++)
    if (arrLevels.checked)
    {
    level_value = arrLevels.value;
    break;
    }


    if none is selected, level_value will be null
     
    koolman, Jun 19, 2008 IP
  11. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #11

    Except it's getElementsByName (plural Elements). :p
     
    zerxer, Jun 19, 2008 IP
  12. koolman

    koolman Peon

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    You are absolutely right
     
    koolman, Jun 19, 2008 IP