Click to See Complete Forum and Search --> : Checkbox status


Vinnie Naydenov
05-14-2003, 11:28 AM
Hello from Canada to all the good folks out there.

I have a checkbox specific question. What script do I need that can check for me if a checkbox has been checked or not, I'd like to be able to refer to the checkbox's name.

I am quite new to JavaScript, so this will definatelly not be my last post, but for now it is all I really need.

Thanks so much!

Vinnie

P.S. JavaScript is new to me, but Java isn't.

Jona
05-14-2003, 12:34 PM
Java is very similar to JavaScript so you shouldn't have much of a problem learning it. :)

<script type="text/javascript">
function validate(checkBox){
if(checkBox.checked){alert("The checkbox is checked");}
else{alert("The checkbox is not checked");}
}
</script>

And your form:

<form action=""><div>
Mr. Check: <input type="checkbox" name="mr_check"><br>
<input type="button" onClick="validate(this.form.mr_check);" value="Is Mr. Check checked?">
</div></form>

pyro
05-14-2003, 12:35 PM
Try something like this:

function testCheckbox() {
if (document.myform.mycheckbox.checked) { // is checked
alert ("yep");
}
else { // is not checked
alert ("nope");
}
}

Jona
05-14-2003, 12:36 PM
lol, Pyro, I beatcha! :D j/k

Vinnie Naydenov
05-14-2003, 01:20 PM
Thank you all, I shall try the code you provided, and let you know if there's trouble. Hope not!

:)