snowdogtrev
snowdogtrev

Reputation: 11

Find a DIV with a certain CSS style using jQuery

I have a bunch of DIV's that all have the same class and style except for 1.

    <div id="0" class="divpage" style="display:none"></div>
    <div id="1" class="divpage" style="display:none"></div>
    <div id="2" class="divpage" style="display:none"></div>
    <div id="3" class="divpage" style="display:none"></div>
    <div id="4" class="divpage" style="display:block"></div>
    <div id="5" class="divpage" style="display:none"></div>
    <div id="6" class="divpage" style="display:none"></div>

I need to find out the id of the div that is 'display:block'. I used the following code but it only returns the first div's id.

var num = $(".divpage").attr("id");

How do I modify this to find the correct id?

Thanks in advance.

Upvotes: 1

Views: 191

Answers (2)

Jay Blanchard
Jay Blanchard

Reputation: 34426

Like this -

var theID = $('.divpage[style="display:block"]').attr('id');

Upvotes: 3

Anton
Anton

Reputation: 32581

using :visible will find the div that is visible

You should not use numbers as id, i think its only valid in html5

var num = $('.divpage:visible').attr('id');

Upvotes: 5

Related Questions