aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes modify this code to require select instead of a checkbox 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 "modify this code to require select instead of a checkbox" Watch "modify this code to require select instead of a checkbox" New topic
Author

modify this code to require select instead of a checkbox

Mark Warner
Greenhorn

Joined: Aug 12, 2010
Posts: 10
I am trying to get additional field forms to appear if a certain item in a form select is selected.

I found this page that offers how to do this if a checkbox is used instead of a select. http://www.tek-tips.com/faqs.cfm?fid=4585

If I have the following select statement

how do I modify the below code to hide the extra fields if green is NOT selected?


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Using jQuery -- and there is no valid excuse not to -- the check is pretty simple:

It's kinda confusing for the value of green to be "blue". Also, simple names like "color" do not make good id values.

Without jQuery, you'll need to find and loop through the options to find the one with the selected attribute. Life is too short for that nonsense.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mark Warner
Greenhorn

Joined: Aug 12, 2010
Posts: 10
Bear Bibeault wrote:Using jQuery -- and there is no valid excuse not to -- the check is pretty simple:

It's kinda confusing for the value of green to be "blue". Also, simple names like "color" do not make good id values.

Without jQuery, you'll need to find and loop through the options to find the one with the selected attribute. Life is too short for that nonsense.


OK thanks for the tips Bear. I am a novice in both jQuery and javascript. I can understand the jQuery line of code you presented. However I assume I would need to rewrite the JavaScript showhidefield() function in jQuery language? Would substituting the one line you provided still make the original JavaScript code valid?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Actually, I'd do the whole thing in a single line using jQuery's .toggle() method.

Are you sure that it's visibility you want to toggle and not display? visibility will still leave room for the hidden element.
Mark Warner
Greenhorn

Joined: Aug 12, 2010
Posts: 10
Bear Bibeault wrote:Actually, I'd do the whole thing in a single line using jQuery's .toggle() method.

Are you sure that it's visibility you want to toggle and not display? visibility will still leave room for the hidden element.


Bear,

Yes I would want to use display instead of visibility. I would not want the blank space left there when blue is NOT selected.

How can I do this in one line using toggle?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

toggle() takes a boolean where true = show, false = hide. So you don't need any if/else statement -- just use the condition as the parameter.

So, something like: $('some-selector').toggle(x == 213)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: modify this code to require select instead of a checkbox