Hmm, wouldn't radio buttons be better? If they all have the same name, only one of them may be checked:
Code:
<!DOCTYPE html>
<html>
<head>
<title>Choices</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
Choice 1<input type="radio" name="choice" value="choice1"><br />
Choice 2<input type="radio" name="choice" value="choice2"><br />
Choice 3<input type="radio" name="choice" value="choice3"><br />
Choice 4<input type="radio" name="choice" value="choice4"><br /><br />
<script type="text/javascript">
(function(){
var choices = document.getElementsByName('choice');
function getchoice(){
for(var i = 0; i < choices.length; ++i){
if(choices[i].checked){
alert('You Chose ' + choices[i].value + '!');
}
}
}
for(var i = 0; i < choices.length; ++i){
choices[i].onclick = getchoice;
}
})();
</script>
</body>
</html>
There's actually a simpler way to go about that, but it demonstrates with two examples how to loop through the choices, which might be necessary in a more complex situation. A simpler way using only one loop:
Code:
<!DOCTYPE html>
<html>
<head>
<title>Simpler Choices</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
Choice 1<input type="radio" name="choice" value="choice1"><br />
Choice 2<input type="radio" name="choice" value="choice2"><br />
Choice 3<input type="radio" name="choice" value="choice3"><br />
Choice 4<input type="radio" name="choice" value="choice4"><br /><br />
<script type="text/javascript">
(function(){
var choices = document.getElementsByName('choice');
function getchoice(){
alert('You Chose ' + this.value + '!');
}
for(var i = 0; i < choices.length; ++i){
choices[i].onclick = getchoice;
}
})();
</script>
</body>
</html>
Bookmarks