strongBAD
strongBAD

Reputation: 331

which number has div with specific class

I would like to have number (int) of ul with class 'currentWeek'. My html look like this:

<div class="calendarContainer">
    <ul>
        <li></li>
        <li></li>
        <li></li>
    </ul>
    <ul>
        <li></li>
        <li></li>
        <li></li>
    </ul>
    <ul class="currentWeek">
        <li></li>
        <li></li>
        <li></li>
    </ul>
    <ul>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</div>

this is how I could all UL's

var weeksCount = widget.find(".calendarContainer").children().length;

But I have no idea how to know that currentWeek is third

Many thanks!

Upvotes: 0

Views: 64

Answers (3)

Adil
Adil

Reputation: 148120

you can use index() method to get the index of element within siblings. It is zero-idexed, means you will get 0 for first element and 1 for second and so on.

Live Demo

$('.currentWeek').index()

Upvotes: 1

VisioN
VisioN

Reputation: 145408

You can use index():

$(".currentWeek").index();

If you need to specify the element relatively to which you need to count use:

$(".currentWeek").index(".calendarContainer ul");

Both approaches return 2, since indices start with zero.

Upvotes: 0

raina77ow
raina77ow

Reputation: 106375

How about this:

widget.find('.currentWeek').index()

Quoting the .index doc:

If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.

You might want to add 1 to result, if you need to get the natural (not 0-based) number of that element.

Upvotes: 4

Related Questions