Hi all,
Say there are two checkboxes on a page (test1 and test2), I want to write a JavaScript code that disables one if the other is checked. Here's what I wrote that didn't work:

<input type="checkbox" name="test1" <script language="Javascript">if (test2.checked = true){document.write('DISABLED');}</script>>

Please help :D!!! Thanks a million!


Do it this way:

<title>Your title</title>
<form action="#" onsubmit="return false;">
<label>Item 1 <input type="checkbox" id="ch0" name="ch0" value="" onclick="if (this.checked) document.getElementById('ch1').disabled=true; else document.getElementById('ch1').disabled = false;"  /></label><br />
<label>Item 2 <input type="checkbox" id="ch1" name="ch1" value="" onclick="if (this.checked) document.getElementById('ch0').disabled=true; else document.getElementById('ch0').disabled = false;" /></label>

Thank you VERY much I will try it out in the morning!

Just curious, what is this <LABEL> tag and is it necessary? I have never seen it before :).

Strangest thing: it works beautifully in my editor's preview window but when I upload it to my site it doesn't???

Any ideas? Thanks :D!

EDIT: Turns out the ID value is absolutely necessary!

This article has been dead for over six months. Start a new discussion instead.