View Full Version : Submit button enable

SeeIT Solutions
05-28-2005, 09:47 AM
Hi, I was just wondering how to make a submit button enabled when a checkbox is clicked (for a terms and conditions kinda thing).


05-28-2005, 09:59 AM
Try this:

<form name="myForm" acton="">
Terms:<input type="checkbox" name="chk" onclick="if(this.checked) document.myForm.sub.disabled=false; else document.myForm.sub.disabled=true;"><br>
<input type="submit" name="sub" disabled="true">

SeeIT Solutions
05-28-2005, 10:03 AM

05-30-2005, 12:37 PM
You can simplify the code by using this.form to refer to the form and using the ! (not) operator.

<input type="checkbox" name="chk" onclick="this.form.sub.disabled=!this.checked" />

05-30-2005, 12:47 PM
Nice shortcut, glenngv


I wonder if it works when value of the attribute is not a boolean, example if it is or it is not null

05-30-2005, 01:44 PM
If you logical NOT a non-boolean value, the result will be a boolean. The non-boolean value will be internally casted to boolean and then it will be negated. See sample below.

alert(!null); //true
alert(!""); //true
alert(!"blah"); //false
alert(!"0"); //false

These make no difference with these shortcuts that we usually use.

var x=null;
if (x) ...

var y;
if (y) ...