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');}
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.
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!
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(); :/
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...
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):
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