Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox controls whether other fields are enabled/disabled - problems 1

Status
Not open for further replies.

jaredc

Programmer
Jun 28, 2002
43
GB
Hi, I'm trying to achieve something wihich I thought would be relatively simple: When a check box is ticked a textfield1 is disabled and textfield2 is enabled (and vice versa when check box is not ticked). The code I've got so far is:
Code:
...
<script language=javascript>
function testbox()
{
if (document.form.check1.checked = true )
  {document.form.textfield1.disabled = true 
   document.form.textfield2.disabled = false 
  }
else 
  {document.form.textfield1.disabled = false
   document.form.textfield2.disabled = true 
  }
}
</script>
...
<form>
<input type=&quot;checkbox&quot; name=&quot;check1&quot; onclick=&quot;javascript:testbox()&quot;>
<input type=&quot;text&quot; name=&quot;textfield1&quot;>
<input type=&quot;text&quot; name=&quot;textfield2&quot;>
</form>
...
The problem I'm getting is that when clicking the checkbox the relevant fields are disabled as expected, but if you try to uncheck the checkbox after it has been previously clicked it won't uncheck the box or disable/enable the textfields.

I've tried using onmouseout, onblur, etc. rather than onclick to start the javascript function but to no avail.

I would be very grateful if someone could point out my (hopefully obvious) coding errors or offer any thoughts. Many thanks in advance!
 
Code:
if (document.form.check1.checked = true )
This line sets the 'checked' status of the checkbox.
Try this:
Code:
if (document.form.check1.checked) {
...
}

Hope this helps
Grzegorz

 
Hi Grzegorz,

Thanks very much - thats done the trick!

Cheers
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top