...

View Full Version : how to get the selected checkbox



snake_eyes
12-25-2007, 12:57 PM
Hello,

I would like to know how to get the selected checkbox, I know how to get the selected radiobox wich the code is listed below:

this.options[this.selectedIndex].value

Thanks in advance...

W. Reagrds

Philip M
12-26-2007, 10:02 AM
Hello,

I would like to know how to get the selected checkbox, I know how to get the selected radiobox wich the code is listed below:

this.options[this.selectedIndex].value

Thanks in advance...

W. Reagrds

this.options[this.selectedIndex].value
does not give the value of a radio button - it gives the value of a selected option in an options array.

You want

document.formname.checkboxname.value

BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's assistance for granted.

snake_eyes
12-27-2007, 12:40 PM
Hello,

Yes exactly my code is for the options, by mistake I sent it as radio box, however Philip M your code is for checkbox but what about if I have the checkbox name such as myname[] how the code should be?

and thanks for your note, but I said thanks in advance :) I mean Any inputs with this post is highly appreciated ;)

W. Regards

Philip M
12-27-2007, 01:30 PM
I hope this example helps you:-

<form name = "myform">
<input name = "myname1" type = "checkbox" value = "Red"> RED <br>
<input name = "myname2" type = "checkbox" value = "Blue"> BLUE <br>
<input name = "myname3" type = "checkbox" value = "Green"> GREEN <br>
<input name = "myname4" type = "checkbox" value = "Yellow"> YELLOW<br><br>
<input type = "button" value = "Click Me" onclick="cboxlist()"><br>
</form>

<script type = "text/javascript">

function cboxlist(){
var choice = "Your colour choice is: ";
var x = "";
for (var i=1; i<5; i++) {
x = eval("document.myform.myname" + i + ".value");
y = eval("document.myform.myname" + i + ".checked");
if (eval("y")) {
choice = choice + " " + x;
}
}
if (choice.length <= 24) {
choice = choice + "NONE";
}
alert (choice);
}

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum