+ 1

plz help to find the error

<html> <head> <title>mark cal</title> </head> <body> <h3>get gender</h3> <label>gender</label> <input type="radio" value="male" name="gen">male <input type="radio" value="female" name="gen"> female <button onclick=" getResult() ">view</button> <script> function getResult() { var gender=document.getElementsByClassName("gen"); if(gender[0].checked) { alert(gender[0].value); } else if(gender[1].checked) { alert(gender[1].value); } else { alert("slect something"); } } </script> </body> </html>

26th Aug 2020, 7:27 PM
vj
vj - avatar
2 odpowiedzi
+ 3
It will be document.getElementByName("gen"); Not document.getElementByClassName("gen");
26th Aug 2020, 7:34 PM
Divya Mohan
Divya Mohan - avatar
+ 1
Use querySelectorAll to collect your radio buttons. var gender = document.querySelectorAll("input[name=gen]");
26th Aug 2020, 7:51 PM
Ipang