• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

modify this code to require select instead of a checkbox

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic