Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 24th, 2006, 04:36 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Enable/Disable Form Elements Question

I'm using JavaScript set the disable property on a textbox if a checkbox is selected, like this:

<input type="checkbox" name="cbquestion1" id="cbquestion1" onclick="javascript:document.form1.txtanswer1.disa bled=false">What is the name of your favorite pet?</input>
<input type="text" id="txtanswer1" name="txtanswer1" value="" size="20" maxlength="20" disabled="true" />

It works great, but now I'd like to add a behavior will basically do the opposite so that if the checkbox is then de-selected after initially being selected, it will re-disable the textbox. If anyone knows how to do this, I'd greatly appreciate the help. Thanks.

KWilliams
 
Old February 24th, 2006, 05:27 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

This will toggle the state on each click:
Code:
onclick="document.form1.txtanswer1.disabled = !document.form1.txtanswer1.disabled
The javascript: preceeding the code is wasted.

--

Joe (Microsoft MVP - XML)
 
Old February 24th, 2006, 06:30 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi joe,

I tried that suggestion, but it just kept the textbox disabled whether or not the checkbox was clicked or not. Since this is in an XSLT doc, this is how I applied your suggestion:

<input type="checkbox" name="cbquestion1" id="cbquestion1" onclick="javascript:document.form1.txtanswer1.disa bled=false!javascript:document.form1.txtanswer1.di sabled=false">What is the name of your favorite pet?</input>
                        <input type="text" id="txtanswer1" name="txtanswer1" value="" size="20" maxlength="20" disabled="true" />

...and I also tried changing the 2nd JS instance to this:

onclick="javascript:document.form1.txtanswer1.disa bled=false!javascript:document.form1.txtanswer1.di sabled=true">

and of course I tried using the exact code that you included, but it still doesn't work. Any other suggestions?

KWilliams
 
Old February 24th, 2006, 07:38 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well XSLT or not that's the correct code. What is the HTML that your XSLT produces?

--

Joe (Microsoft MVP - XML)
 
Old February 28th, 2006, 11:28 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I made a mistake by including this code in the onclick event:
onclick="javascript:document.mycounty_reg.txtanswe r1.disabled=false!document.mycounty_reg.txtanswer1 .disabled=false
...instead of the exact code that you supplied:
onclick="document.mycounty_reg.txtanswer1.disabled = !document.mycounty_reg.txtanswer1.disabled

Sorry about that. I just needed to change "disabled=false" to "disabled", and it worked great. Thanks again for your help Joe:)

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
Enable / Disable Form Fields Automatically jackyam Javascript How-To 10 March 17th, 2008 04:46 AM
Disable/Enable elements ldoodle Javascript How-To 3 June 7th, 2005 05:48 AM
Question on dynamic changes to other form elements savoym Javascript 1 October 27th, 2004 10:17 AM
Disable Form Elements Hotwax ASP.NET 1.0 and 1.1 Professional 2 October 31st, 2003 08:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.