JavaScript checking radio buttons
42,456
Solution 1
Radio button html:
<input type="radio" name="radionbutton" value="1" id="button1"/>
<input type="radio" name="radionbutton" value="2" id="button"/>
Javascript:
var button1 = document.getElementById("button1");
var button2 = document.getElementById("button2");
if (button1.checked){
alert("radio1 selected");
}else if (button2.checked) {
alert("radio2 selected");
}
Solution 2
http://jsfiddle.net/Squeegy/KCT8h/
html
<input type="radio" name="zing" id="foo" checked/>
<input type="radio" name="zing" id="bar"/>
js
if (document.getElementById('foo').checked) {
alert('foo');
} else {
alert('bar');
}
Author by
Maximillian Nahbai
Updated on February 13, 2020Comments
-
Maximillian Nahbai about 4 years
Ok, this is really a simple question but I am really incompetent at JavaScript.
Basically all I have a form with 2 radio buttons on them.
I need a JavaScript statement which basically says
If radiobutton1 is selected then document.write ("radiobutton1selected") else if radiobutton2 is selected then document.write ("radiobutton2selected")
There are similar questions on here i accept but they are all alot more advanced than what i need.
-
Maximillian Nahbai over 11 yearsWow... it's that simple. I was trying stuff similar to that but couldn't get it. Thanks a bundle
-
Maximillian Nahbai over 11 yearsWow... it's that simple. I was trying stuff similar to that but couldn't get it. Thanks a bundle