aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Enabling a selectList via javascript does not work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Enabling a selectList via javascript does not work" Watch "Enabling a selectList via javascript does not work" New topic
Author

Enabling a selectList via javascript does not work

Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hi All,
I have been able to disable a selectList via an onklick (spelling onklick wrong on purpose, javaranch seems to be unable to parse it here) event of a radio button. However, my javascript method which should also enable the selectList does NOT work. Once this list is disabled, I cannot re-enable it via javascript.

In the radio button, I have the following onklick event:


These onklicks call this javascript


When 'true' is passed to disable the drop down, it works. When 'false' is passed to the javascript method to enable the drop down, it does not work. I see from the alert message the 'false' is received. Then the second alert returns 'true' since that is the current state of the select box. The last alert, I would assume would return false, since that is what it was just set to, but it returns a 'true', and the box does stay disabled.

Can you only disable things in javascript and not enable them?
Thanks,
Kim
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Originally posted by Kim Kantola:
spelling onklick wrong on purpose, javaranch seems to be unable to parse it here
If you are working in HTML, you should have a good working knowledge of HTML Entities. With them, you can easily defeat the posting filters: onclick onclick onclick.

document.form1._selectedUnit.disabled=enabled;

Are you 100% certain that document.form1._selectedUnit is correctly referencing the element? In any case, that's a really old-fashioned way of doing things. You should be assigning an id to the element and using document.getElementById() to reference the element.

Make that change to eliminate that as a source of error and if you're still having difficulties post back and we'll pursue it further.
[ August 04, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
I guess there is a difference between being able to get by the onclick. filters and having the patience to!

I had previously been using document.getElementById(), and then switched to using the control name specifically (_selectedUnit) to try to make things simpler and rule out the fact that maybe I wasn't operating on the correct element, so that can rule out that problem.

I ended up getting by the problem by adding the javascript right to the onclick call, and now it works, not sure why. Here is what I have now:


This works. But doing this inside a javascript function as I showed in my previous post did not. I don't understand why. I also tried defining a javascript boolean instead of setting it to the string value of false and that didn't work either. I am fine now that i have it working but am still curious if anyone knows any reason why that might have not worked.
[ August 04, 2008: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Originally posted by Kim Kantola:
I had previously been using document.getElementById(), and then switched to using the control name specifically (_selectedUnit) to try to make things simpler and rule out the fact that maybe I wasn't operating on the correct element, so that can rule out that problem.
You've clearly gone in the wrong direction. Fetching the element by id is exact. The document.form mechanism is fragile and should be avoided.

but am still curious if anyone knows any reason why that might have not worked.
Well, since we don't have access to that code, who can say?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6


Eric
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Thank you Eric. Using false instead of 'false' did the trick.
Going to brush up on javascript . . .
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hi,
Wanted to add one more note to this as it may be a help to someone searching on this topic.
I was having trouble with the document.getElementById() call for my radio button element because struts <html:radio tag does not support having an id defined.
I found that the javascript worked if I used to replace

then my javascript worked, however, this did NOT work :
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Well yeah, if Struts won't let you put an id on an element (big :roll: ) then you can't use an id.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enabling a selectList via javascript does not work
 
Similar Threads
enabling and disabling a button using check box
I want to submit the button but not refresh the page
How to disable html:checkbox based on html:radio selection
Enabling and disabling
making the textbox readonly