1

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.

2 Answers2

5

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');
Anton
  • 32,245
  • 5
  • 44
  • 54
  • 1
    anyway consider using and toggling a .hidden class instead of using inline css – David Fregoli Feb 06 '13 at 14:28
  • "In CSS2, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [A-Za-z0-9] and ISO 10646 characters 161 and higher, plus the hyphen (-); they cannot start with a hyphen or a digit." see http://stackoverflow.com/questions/11100048/css-style-not-recognizing-numbers – AlexChaffee Feb 06 '13 at 14:29
3

Like this -

var theID = $('.divpage[style="display:block"]').attr('id');
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 1
    This is the right answer to the question as posed, but I think he really wanted to ask the question answered by Anton :-) – AlexChaffee Feb 06 '13 at 14:27