Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript checking radio buttons

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.

like image 249
Maximillian Nahbai Avatar asked Mar 31 '26 14:03

Maximillian Nahbai


2 Answers

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");
}
like image 100
Ibu Avatar answered Apr 03 '26 02:04

Ibu


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');
}​
like image 26
Alex Wayne Avatar answered Apr 03 '26 02:04

Alex Wayne