4

I have a form that needs to display specific select options ("locations") based on the user's ZIP, which is in a number field above. In this example, I need the option "Out of Range" to hide and the "In Range" to show when the user enters "12345" in the input.

This is my HTML:

<!--Zip-->
<div class="zip-field">
    <label for="zip">Zip Code</label>
    <input type="number" id="zip" name="zip" />
</div>

<!--Location-->
<div class="location-field">
    <label for="location">Location</label>
    <select id="location" name="location">
        <option value="In Range">In Range</option>
        <option value="Out of Range">Out of Range</option>
    </select>
</div>

This is my jQuery:

$('#zip').on('change',function(){
    if ( $(this).val() == "12345" ) { 
        $("#location option[value='In Range']").show();
        $("#location option[value='Out of Range']").hide();
    }
});

Should be pretty straightforward, but no cigar.

1
  • 1
    hiding a option element does not have cross browser support... you need to remove them Commented Sep 26, 2013 at 1:08

3 Answers 3

2

There are two things you need to change here:

  1. You need to set an event handler to the zip input element. $('#zip').val( ... ); only sets the value once when that line is executed.

  2. You need to select the option better. $("#location option[value='In Range']").show(); will not show the option you want. You have to set the value of the selector input to a value that matches the option you want.

Change your javascript to this:

$("#zip").on('blur', function(){
   if ( $(this).val() == "12345" ) { 
       $("#location").val("In Range");
   }else{
       $("#location").val("Out of Range");
    }
});

Notice that I'm using $('#zip').on('blur', ...); to register an event handler, setting it to the blur event and passing in a function to be executed when that event fires.

Then I set the value of the location selector input to the correct value of the option you want to select.

DEMO

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

2 Comments

This is awesome. Thank you for the conciseness! I will definitely use this later. But I need to hide it so the user can still have the option to choose one. Sorry I shouldve made that clear in example. . .
Thanks, I misunderstood what you meant as 'hide'.
1

Hiding an option won't work across browsers, it is same as binding event to option elements you can do only very limited thing with them. instead remove them and cache them for later use.

$(function(){
    var $location = $('#location');
    $location.data('options', $location.find('option')); //cache the options first up when DOM loads
    $('#zip').on('change', function () { //on change event
        $location.html($location.data('options')); //populate the options
        if ($(this).val() == "12345") { //check for condition
            $location.find("option[value='Out of Range']").remove(); //remove unwanted option
        }

    });
});

Fiddle

9 Comments

Why do this when you can pick the option you want by setting the value of the selector to match an option value? E.g. $("#location").val("In Range");
@RickHanlonII See OP. He wants to hide them. Read the question completely. If it was just setting a value he might not have even asked this as a question here.. pretty straight forward right...
@RickHanlonII Yeah I need the user to still have the ability to choose one from a list. :( I should have mentioned that in the example. Lemme try PSL's. . .
@Andrew yeah with this you can choose , only thing is when it is out of range you dont event want to show other options to the user right?
@PSL correct. Almost done trying it out on my own. . . thanks so much for the help! I'll let you know what happens.
|
1

You should monitor the the value as it changes using the method below:

$('#zip').on('keyup',function(){
    $("#location").val('Out Of Range');
    if ( $(this).val() == "12345" ) { 
        $("#location").val('In Range');
    }
});

The on function binds an event listen to that Element. The keyup event listens for when the key is released inside the your field. You can then compare the value to what ever and show / hide as needed.

4 Comments

Thank you! I updated my code to reflect yours. . . but I still cannot get it to work. :/
@Andrew that's because of the way you're "showing" the option you want. See my answer for the right way to do this.
Hes right, I didnt look close enough at your question to realize your selectors where off. Update my answer
I used your 'keyup' function in my final product though! I didn't even know that existed, so thank you very much. ^_^

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.