views:

49

answers:

1

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.

+2  A: 

$("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");
rahul
doesn't seem to be working
mickyjtwin
Take a look at this. http://jsbin.com/iropo3
rahul
It does work fine. Problem was that there are two separate lists, and it was applying to the end as set. Just need to select based on list type.
mickyjtwin