Question

<ul>
    <li class="res"></li>
    <li class="res"></li>
    <li class="res selected"></li>
    <li class="res"></li>
</ul>

Using $('ul li.selected').length I can check total number of "selected elements" BUT I'd like to check the serial number of the "selected element" (in that case 3)
Can you help me do that.
Thanks

Was it helpful?

Solution

What exactly do you mean by serial number?

The content?

 alert (  $('ul li.selected').text() );

The index?

 var listItem = $('ul li.selected');
 alert('Index: ' + $('ul li').index(listItem));

OTHER TIPS

You can use the index() method like this:

alert('Index: ' + $('li').index('.selected'));

Try

$("ul li.selected").index() + 1

Since .index() is 0 based you need to add 1 to get your desired result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top