Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
172
rated 0 times [  177] [ 5]  / answers: 1 / hits: 22177  / 11 Years ago, sun, march 24, 2013, 12:00:00

I wonder if anyone can help me out, I'm stuck where I need to write a little code that goes like this.



Question : did you like the training?

<input type=radio name=q4 value=Yes>Yes
<input type=radio name=q4 value=No>No


whatever is checked I want to pass it to a javascript variable.


More From » html

 Answers
50

I think you could do something like this:



var ch = document.getElementsByName('q4');

for (var i = ch.length; i--;) {
ch[i].onchange = function() {
alert(this.value);
}
}


http://jsfiddle.net/EUErP/



So you subscribe to to change event and can use selected value somehow.



Alternatively you can use querySelector (querySelectorAll) methods. Here is an example of how you can get checked element value:



var checked = document.querySelector('[name=q4]:checked');


http://jsfiddle.net/EUErP/2/


[#79390] Friday, March 22, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaceyr

Total Points: 510
Total Questions: 97
Total Answers: 116

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
kaceyr questions
;