mickyjtwin
mickyjtwin

Reputation: 4990

jquery :last missing multiple classes

I have an unordered list, and am using jquery to find the last ul and apply a class.

jQuery("ul.class1:last").addClass("lastUi");

The html is as follows:

<ul class="class1"></ul>
<ul class="class1"></ul>

This is working fine, however it has now changed in that another class is being added to the list so the html looks like:

<ul class="class1"></ul>
<ul class="class1 class2"></ul>
<ul class="class1"></ul>
<ul class="class1 class2"></ul>

The jQuery code is now not picking up the last ul with both class1 and class2 as the last element and isn't adding the lastUI class.

Upvotes: 2

Views: 201

Answers (1)

rahul
rahul

Reputation: 187030

$("ul.class1, ul.class2").filter(":last").addClass("lastUi");

The above one will select last element even if it has only class 2.

Hope this works fine for you

$("ul[class~=class1]:last").addClass("lastUi");

Upvotes: 2

Related Questions