View Full Version : How do i run a function ONLY when a checkbox is UNCHECKED?
ventura
07-11-2002, 11:22 PM
i've been using the onClick handler but that runs the function every time the checkbox is checked.
i have a checkbox that is initially checked, and if the user unchecks it i want to run a function, but if they check it again, i don't want to run the function.
in other words, i ONLY want to run the function when the box is UNCHECKED.
thanks.
poccil
07-11-2002, 11:36 PM
You can check whether a checkbox is checked by using the checked attribute of the form element.
In your case, the event handler should be written like the following:
onclick="if(!this.checked)DoSomething();"
ventura
07-11-2002, 11:40 PM
thanks poccil, but when i try to recheck the checkbox, it doesn't let me. is there a way to fix this?
thanks,
joh6nn
07-11-2002, 11:59 PM
can we see the code you're using?
ventura
07-12-2002, 12:09 AM
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
function appendHidden(ckvalue) {
var str = document.form;
var ckvalue;
str.hiddenfield.value = str.hiddenfield.value + '-' + ckvalue;
alert(str.hiddenfield.value);
return false;
}
</script>
</head>
<body bgcolor="white">
<form name="form" method="post" action="javascript:;">
<input type="hidden" name="hiddenfield" value="">
<p>
<input type="checkbox" name="box1" value="box1unchecked" checked onClick="if (!this.checked) { appendHidden(this.value);} else { return false;}">
box1<br>
<input type="checkbox" name="box2" value="box2unchecked">
box2<br>
<input type="checkbox" name="box3" value="box3unchecked" checked onClick="appendHidden(this.value);">
box3<br>
<input type="checkbox" name="box4" value="box3unchecked">
box4<br>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
joh6nn
07-12-2002, 12:32 AM
<input type="checkbox" name="box1" value="box1unchecked" checked onClick="if (!this.checked) { appendHidden(this.value);}else { return false;}">
i think you need to get rid of the bit of bold. let me know if that does the trick.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.