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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional Disable

Status
Not open for further replies.

phish20002003

Programmer
Sep 5, 2006
99
US
I am fairly new to javasctipt. I'm trying to disable a button if a second button is named let's say Activate. This is what I have so far.

<HTML>
<HEAD>
</HEAD>
<SCRIPT LANGUAGE="JavaScript">
if(document.form.button1.value =="Activate")
{document.form.Button2.disabled=true;}
</SCRIPT>
</HEAD>
<BODY TEXT="000000" BGCOLOR="FFFFFF">
<form>
<td align="center" valign="top" colspan="1"><input type="button" name="button1" value="Activate"></td>
<td align="center" valign="top" colspan="1"><input type="button" name="button2" value="test"></td>
</form>
</BODY>
</HTML>

-Phish
"Why do you need to think? Can't we just sit and go budumbudumbudum with our lips for a bit?" - Mostly Harmless
fsm.png
 
On this:
Code:
document.form.Button2.disabled=true

The name of the button is button2, not Button2.

Also you are trying to run that script before the elements even exist.

You need to drop everything in the script tags below the HTML that creates the buttons, or better yet, disable button2 as it loads, since button1's value is Activate.

[monkey][snake] <.
 
thanks, the problem is button1's value will not always be activate. Sometimes it will be deactivate.

-Phish
"Why do you need to think? Can't we just sit and go budumbudumbudum with our lips for a bit?" - Mostly Harmless
fsm.png
 
Then you should either put that Javascript on the page AFTER the buttons are created, or inside a function and call the function in the page's onload event handler.

Example:
Code:
<form>
<td align="center" valign="top" colspan="1"><input type="button" name="button1" value="Activate"></td>
<td align="center" valign="top" colspan="1"><input type="button" name="button2" value="test"></td>
<SCRIPT type="text/javaScript">
if(document.form.button1.value =="Activate")
{document.form.button2.disabled=true;}
</SCRIPT>
</form>

Lee
 
If the button is supposed to enable and disable, you need to call a function whenever the value of button1 is changed.

Your function would pretty much look like the script you already have written, with an else.

Code:
<script type="text/javascript">
   function disableButton() {
      if (document.form.button1.value =="Activate") {
         document.form.button2.disabled = true;
      }
      else {
         document.form.button2.disabled = false;
      }
   }
</script>
</form>

Like I state above, function would be called whenever the value of button1 is changed.





[monkey][snake] <.
 
Just looked over the code I'd copied and pasted. The form in the original example has no name, so should be accessed like this (along with the elements array):
Code:
document.forms[0].elements['button1'].value

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top