File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

modify this code to require select instead of a checkbox

 
Mark Warner
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Warner
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic