2

I'm using jQuery Mobile and I have a select box with yes and no as the options. I want a text input field to display when they select yes and it to disappear when they select no.

jsFiddle link

<fieldset data-role="controlgroup">
<label for="joint">Is this a joint gift?</label>
<select name="slider" id="joint" data-role="slider">
    <option value="no">No</option>
    <option value="yes">Yes</option>
</select> 
<div id="enter_joint">
    <label for="other_joint">Spouse Name:</label>
    <input name="other_joint" id="other_joint" value="" data-theme="d" />
</div>
</fieldset>

I want to have use jQuery to only display #enter_joint when the select is set to Yes.

5 Answers 5

5
$('#enter_joint').hide();
$('#joint').change(function(){
    $('#enter_joint').toggle('show');
});

http://jsfiddle.net/aNrea/4/

Sign up to request clarification or add additional context in comments.

Comments

1

AlienWebguy's answer will work, but I tend to err on the side of "check the value". So here's a bit more that checks the value and then determines whether or not to hide the text field.

$(document).ready(function() {
    $("#enter_joint").hide();
    $("#joint").val('no');
    $("#joint").change(function() {
        if ($(this).val() == 'yes') {
            $("#enter_joint").show();
        } else {
            $("#enter_joint").hide();
        }
    });
});

jsfiddle

2 Comments

using a toggle function is more efficient where a less code give you the same functionality . $('#enter_joint').toggle('show');
@Saifuddin and in this case, toggle would probably be the best route since there are only 2 answers (which is why I +1'd the AlienWebguy's answer). Some days you want to be explicit despite the circumstances. :)
1
$(document).ready(function(){
    $('#enter_joint').hide();
    $('#joint').change(function(){
        val = $(this).val();

        if(val == 'yes')
        {
            $('#enter_joint').show();
        }

        if(val == 'no')
        {
            $('#enter_joint').hide();
        }
    });
});

Comments

0

try this

$('#joint').change(function(event) { 
    if($('#joint').val() === 'yes') { 
        $('#enter_joint').hide(); } 
    else { $('#enter_joint').show();
          }
});

Comments

0

Alternate Syntax:

$('#enter_joint').hide();
$('#joint').change(function(){
    show = $(this).val() == 'yes';
    $('#enter_joint').toggle(show);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.