Forum Moderators: open

Message Too Old, No Replies

Help with TextBox onChange script

         

doctormelodious

11:28 pm on Jun 22, 2004 (gmt 0)

10+ Year Member



Greetings,

I am learning my way around JavaScript. Based on info I found on the web, I set up this script to check the status of a checkbox when it is changed. Can anyone tell me why it's not working? The script is in the <head> and the form is in the <body>. It doesn't seem to be executing at all; even the alert('HELP!') doesn't happen. Doesn't work for onClick either.

Thanks!
Perry


<script language="JavaScript">
<!-- Begin
function checkCheckBox() {
if(document.forms[simple].checkbox[0].checked){
alert('It's checked');
}
else{
alert('Not checked.');
}
alert('HELP!');
// -->
</script>

.
.
.

<form name=simple>
<input type="checkbox" value="1" name="simpleFormatEnable" onChange="javascript:checkCheckBox();">
.
.
.
</form>

doctormelodious

2:23 am on Jun 23, 2004 (gmt 0)

10+ Year Member



Got it working now.


<script language="JavaScript">
<!-- Begin
function checkCheckBox() {
if(document.forms.contentEditForm.simpleFormatEnable.checked){alert('true');}
else {alert('false');};
}
// -->
</script>

dcrombie

9:03 am on Jun 23, 2004 (gmt 0)



I think you might still have issues in some browsers with that code. You should use 'onClick' instead of 'onChange' and you don't need to specify "JavaScript:" in the event handler.

<SCRIPT type="text/javascript">  
function checkCheckBox(box)
{
if(box.checked) {
// box is checked
} else {
// box not checked
}
}
</SCRIPT>

<FORM ...> 
<INPUT type="checkbox" ... onClick="checkCheckBox(this);">
...
</FORM>

doctormelodious

4:17 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



Hi dcrombie,

Thanks much! Passing "this" as an argument is very helpful.

Perry