Scott
Scott

Reputation: 4200

JQuery find which one is visible

<select id="milesAway">
    <option id="5" value="5">5 miles</option>
    <option id="10" value="10">10 miles</option>
    <option id="25" value="25">25 miles</option>
    <option id="50 value="50">50 miles</option>
    <option id="100" value="100">100 miles</option>
    <option id="250" value="250">250 miles</option>
    <option id="500" value="500">500 miles</option>
</select>
<input type="text" id="zipCode" />
<input type="text" id="cityState" class="cityState" />

So at any point in time, only one of these 3 available inputs will be shown. They are all controlled by .show() and .hide()

I do I use JQuery to find out which one is the one that is shown and get it's id.

Thanks!

Upvotes: 5

Views: 3393

Answers (4)

Hussein
Hussein

Reputation: 42818

you can do

$(':input:visible') //This will selects all inputs on the page. You can narrow this down by specifying an ID that wraps the input you are looking or.

You can also check if input is visible by doing

$('#cityState').is(':visible') 

Upvotes: 3

johnlemon
johnlemon

Reputation: 21449

http://jsfiddle.net/RSGrW/1/

First update after reading comments:

var id = $("#milesAway:visible option:selected, #zipCode:visible, #cityState:visible").attr('id');

Because I saw you put the ids inside the option, I figured you would like that returned. This is the alternative for just the input or select id.

var id = $("#milesAway:visible, #zipCode:visible, #cityState:visible").attr('id');

Upvotes: 0

Robert Ross
Robert Ross

Reputation: 1925

Here ya go:

$('select:not(:visible)');

I'd suggest adding a class to each of the elements to easily select them:

$('.toggleInputs:not(:visible)');

Upvotes: -1

Jonathan
Jonathan

Reputation: 76

$('option:visible');

would match all option elements that are not hidden, by hidden I mean display:none, I don't think visibility:hidden counts! you would have to double check this in the jQuery documentation under the "Selectors" section

Upvotes: -1

Related Questions