views:

388

answers:

4

I have the following code:

<select>
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Other">Other</option>
</select>

<input type="text" id="other" />

What I want to do is using jQuery make the textbox below hidden by default, and then show it if a user selects the other option from the dropdown.

+2  A: 
<select id="sel">
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Other">Other</option>
</select>

<input type="text" id="other" style="display: none;" />

$('#sel').change(function() {
    $('#other').css('display', ($(this).val() == 'Other') ? 'block' : 'none');
});
David Hedlund
Although Cameron didn't ask to hide the textbox if any other option was selected...
ScottE
@ScottE: this is true, but I'd be very surprised if that's not what OP intended.
David Hedlund
This one works great thanks.
Cameron
A: 

Try this:

<select id="selectbox_id">
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Other">Other</option>
</select>

<input type="text" id="other" />

JQuery:

$(function(){
  // hide by default
  $('other').css('display', 'none');

  $('selectbox_id').change(function(){
   if ($(this).val() === 'Other') {
     $('other').css('display', 'block');
   }
 });
});
Sarfraz
why down vote...............?
Sarfraz
A: 

This is what I have:

            $(function () {
                $('#other').css('display', 'none');

                if ($('.ebpselect').val() === '21') {
                    $('#other').css('display', 'block');
                }
                else {
                    $('#other').css('display', 'none');
                }
                $('.ebpselect').change(function () {
                    if ($(this).val() === '21') {
                        $('#other').css('display', 'block');
                    }
                    else {
                        $('#other').css('display', 'none');
                    }
                });

            });
Cameron
A: 

No need for any css here.

$('#sel').change(function() {
    var selected = $(this).val();
    if(selected == 'Other'){
      $('#other').show();
    }
    else{
      $('#other').hide();
    }
});
gmcalab
sure never heard anyone say "no need for css" before. why on earth would you ever want to avoid using css?
David Hedlund
@David Hedlund, what's the point of using css for showing and hiding elements when jQuery has the functionality built in. I guess if you still want to do calculations with an abacus go ahead, but using show/hide is much more elegant.
gmcalab
Actually, I found David Hedlund's answer to be more elegant. If you look at the content rendered in firebug you'll see that css display is being used under the covers anyway.
ScottE
@gmcalab: i went with the `display` option to be able to use a ternary operation instead of a several-line `if` statement.
David Hedlund