Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  130] [ 7]  / answers: 1 / hits: 17728  / 13 Years ago, fri, december 2, 2011, 12:00:00

I need to select a radio button dynamically.



i) I have created few radio buttons dynamically, i.e. in a function using:



function funcName()
{
Something.innerHTML = '<input type = radio name = rName id = 1 />' + '<input type = radio name = rName id = 2 />' + '<input type = radio name = rName id = 3 />';

}


ii) I need to select a radio button dynamically, in another function:



function funcNAme(someVar)
{
document.getElementById(someVar).checked = true;
}


But this does not work, i.e. the radio button doesn not get selected. However, 'disabled' works fine.
Also:



alert(someVar); 


returns the appropriate desired value.


More From » function

 Answers
22

The syntax is as follows :



element.checked = true;


So you need to use



document.getElementById(someVar).checked = true;


See here -> HTMLInputElement



Working example -> http://jsfiddle.net/p5yca/



And please please do not use numbers for IDs of elements -> http://www.w3.org/TR/html401/types.html#type-name


[#88785] Thursday, December 1, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sidneyh

Total Points: 118
Total Questions: 108
Total Answers: 105

Location: Mali
Member since Fri, Jun 18, 2021
3 Years ago
sidneyh questions
Tue, Jun 7, 22, 00:00, 2 Years ago
Wed, Apr 13, 22, 00:00, 2 Years ago
Wed, Aug 12, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Fri, Apr 24, 20, 00:00, 4 Years ago
;