Jquery: last missing multiple classes

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

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

      

html looks like this:

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

      

This works great, however it has now changed in that another class is added to the list, so the html looks like this:

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

      

JQuery code now does not pick up the last ul with both class1 and class2 as the last element, and does not add the lastUI class.

+2


a source to share


1 answer


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

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



Hope this works for you.

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

      

+2


a source







All Articles