Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  147] [ 7]  / answers: 1 / hits: 24498  / 13 Years ago, thu, december 29, 2011, 12:00:00
<input type=radio value=0 name=type checked=yes />
<label>Type 0</label>
<input type=radio value=1 name=type />
<label>Type 1</label>


and js:



var type = this.type.value;
alert(type);


How to fix it ?


More From » html

 Answers
23

In what context does that JS code run? If this is the radio button in question then this.value will return the value.



If your question is How do I get the value of the currently selected radio button in the 'type' group? then you may need to do something like this:



function getCheckedRadioValue(radioGroupName) {
var rads = document.getElementsByName(radioGroupName),
i;
for (i=0; i < rads.length; i++)
if (rads[i].checked)
return rads[i].value;
return null; // or undefined, or your preferred default for none checked
}

var checkedValue = getCheckedRadioValue(type);


(It would be easier with .querySelector() or .querySelectorAll(), but not all browsers support them.)


[#88332] Tuesday, December 27, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
madelyn

Total Points: 449
Total Questions: 100
Total Answers: 100

Location: Seychelles
Member since Fri, May 7, 2021
3 Years ago
madelyn questions
Wed, Jul 28, 21, 00:00, 3 Years ago
Wed, Jul 14, 21, 00:00, 3 Years ago
Sat, Nov 7, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;