<li class="numeric optional" id="contest_max_retweet_input"><label for="contest_max_retweet">Winning retweet number</label><input id="contest_max_retweet" name="contest[max_retweet]" size="50" type="text" /></li>
<li class="numeric optional" id="contest_numofwinners_input"><label for="contest_numofwinners">Number of winners (1-100)</label><input id="contest_numofwinners" name="contest[numofwinners]" size="50" type="text" /></li>
How can I use JQuery to Hide the field: contest_numofwinners_input if there is a value specified by the user in the field: contest_max_retweet_input?
I would toggle based on negation of value at change. I'd also go ahead and trigger the event handler at load to give an initial show/hide state (using a name-spaced event to avoid causing other bound functionality executing).
(Edit: I like what Brandon Boone did regarding keyup for instant action and added a comment to the event name on my code. It will, however, add a little overhead as the function is run for every stroke on the keyboard as opposed to when the field is blurred.)
Demo:
http://jsfiddle.net/JAAulde/fUKfb/3/
Code:
Something like
The problem with change is that you have to leave the input (blur) in order for the function to fire. That's why I prefer keyup.
David Thomas's answer certainly works well and I would use this for simple cases.
However, I've been working on a plugin that does this with multiple conditions and with multiple elements. I would like to share that now.
Here is the jsfiddle that works for your problem: http://jsfiddle.net/natedavisolds/rVehh/3/
Essentially, load the plugin then on load:
The nice thing is that you can chain the reactIf statement to a set of rules.
Here's the plugin.
You could try something like:
JS Fiddle demo
This hides only the
input
element itself, not the containingli
, or correspondinglabel
, element.