This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes enable disabled input using js Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "enable disabled input using js" Watch "enable disabled input using js" New topic
Author

enable disabled input using js

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i have two text inputs that i want to be disabled unless a particular radio button is checked. how do i do this?


SCJP
Visit my download page
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
Hi Randall,
afaik is there no special js-function for enabling an disabled (set readonly) inputfield.
I'd do it this way:

Only tested in IE6 !
hth cb
[ May 25, 2003: Message edited by: Christian Baron ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Acyually it can be done using the disabled attribute of the element
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

it is a radio button not a checkbox. i will look over erics code though. i see...the documentation didnt say you could assign true or false to the disabled attribute
i guess i could put an onclick in each of my radio buttons and, in the script, set the attribute true or false depending on the value of the radio button. unless there is an easier way
[ May 25, 2003: Message edited by: Randall Twede ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The code should not be that much different from the radio button, yeah, the disabled attribute can be handled that way, but the readonly on can not be changed with true or false which makes me wonder why it exsists!
Eric
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i guess i could put an onclick in each of my radio buttons and, in the script, set the attribute true or false depending on the value of the radio button. unless there is an easier way
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

got it...thanks eric..you too christian..the script ended up looking like this:

tab order works right and everything
[ May 25, 2003: Message edited by: Randall Twede ]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

disabling inputs is the exception rather than the norm, so if you are curious, this is a homework assignment to make a complaint form. i will post the entire code for any that are curious about why i wanted to disable the two text inputs.

[ May 25, 2003: Message edited by: Randall Twede ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I was thinking of a trickier way to do it without all of the onclicks, I know a way to do it, but it adds alot of code. Well see if I can scrounge something up.
but I would use onblur or onfocus instead of onclick incase they use the tab key and arrow keys!
[ May 25, 2003: Message edited by: Eric Pascarello ]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

it seems to work fine with the tab key...if the right radio button is not selected, it skips the textboxes
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
what I am saying is this
tab to the radio buttons, push the arrow key and select the right one that will show the field. then push tab....see what i am talking about?
Eric
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

oh..i see what you mean, but it works anyway (at least in IE). if i tab to the radio buttons, then arrow to the one that enables the two textboxes, then hit tab, the first (formerly disabled) textbox gets the focus.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Just thought I would point that out, do not want you to loose points...lol
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

lol...the script is a "bonus" feature not actually required. i think i will change it as suggested anyway as it might not work correctly with all browsers when not using a mouse.
 
 
subject: enable disabled input using js