I want my code to get the checked radio button value when the user clicks the save button. I have the code laid out but I am unsure how to loop through the radio buttons since they don’t have a class id? Here’s what I have:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
saveButton.onclick = function() {
// Go through shirtColor rabio buttons to see which one is ticked
for (var i = 0; i < shirtColor.length; i++) {
if (.checked = true) {
// Set shirtColor variable to selected color
var shirtColor = .checked;
// Only one value can be selected so we're done
return;
}
}
};
</script>
</head>
<body>
<p>Shirt Color:
<input type="radio" name="shirtColor" id="red" value="red" />
Red
<input type="radio" name="shirtColor" id="green" value="green" />
Green
<input type="radio" name="shirtColor" id="blue" value="blue" />
Blue</p>
<p>
<input type="button" id="saveButton" value="Save">
</p>
</body>
</html>
With plain Javascript you could do:
See this Fiddle.