Tim Kruger
Tim Kruger

Reputation: 897

How to find visible class based on partial id and then return full id in jquery

How would I find the visible class based on partial id and then return the full id in jquery, here is some of my code for you to get a better understanding.

<div class="visible" id="fares-and-payments-1-10kms">
</div>

<div class="hidden" id="fares-and-payments-11-20kms">
</div>

<div class="hidden" id="fares-and-payments-21-30kms">
</div>

So basically if you take the snippet above I want to return the "fares-and-payments-1-10kms" id as that is the div that has a class that is visible.

The most elegant and efficient way would be awesome.

Upvotes: 3

Views: 68

Answers (1)

adeneo
adeneo

Reputation: 318182

You could use the attribute starts-with selector, in combination with the class

var elem = $('[id^="fares-and-payments"].visible');

That gets you the element, if you need the ID, you'd just do elem.prop('id')

Upvotes: 4

Related Questions