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 Answers 2

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');
Sign up to request clarification or add additional context in comments.

2 Comments

anyway consider using and toggling a .hidden class instead of using inline css
"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 stackoverflow.com/questions/11100048/…
3

Like this -

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

1 Comment

This is the right answer to the question as posed, but I think he really wanted to ask the question answered by Anton :-)

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.