+ 4

I supposed to take the value of two radio buttons with one id but it is not working correctly.

I have created two radio buttons with all the different values, but alas it is not working. <input type="radio" value="1" id="a" name="some"> <input type="radio" value="2" id="a" name="some"> <input type="radio" value="3" id="a" name="some"> <button onclick="myFun()"></button> function myFun(){ var myVariable = document.getElementById("a").value; // lets say i need value 2... if(myVariable == '2'){ alert(”done”); } code is correct but still it is not working? why? same problem give a upvote please....😕

26th Jan 2018, 6:14 AM
Vivek Tiwari
6 Answers
+ 3
ok sir let me try....
26th Jan 2018, 6:17 AM
Vivek Tiwari
+ 2
brother it is still not working....😖 here is the code.... <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <input type="radio" value="1" class="a" name="some"> <input type="radio" value="2" class="a" name="some"> <input type="radio" value="3" class="a" name="some"> <button onclick="myFun()">something</button> <script> function myFun(){ var ans = document.getElementsByClassName("a").value; if(ans == '2'){ alert("done"); } } </script> </body> </html>
26th Jan 2018, 6:26 AM
Vivek Tiwari
+ 1
id element must will be unique
26th Jan 2018, 6:16 AM
Demid Metelnikov
Demid Metelnikov - avatar
+ 1
Use class attribute
26th Jan 2018, 6:16 AM
Demid Metelnikov
Demid Metelnikov - avatar
+ 1
I don't know correct answer!((.😱
26th Jan 2018, 6:32 AM
Demid Metelnikov
Demid Metelnikov - avatar
0
OK)
26th Jan 2018, 6:41 AM
Demid Metelnikov
Demid Metelnikov - avatar