Yasir Laghari
Yasir Laghari

Reputation: 10744

jquery(selector) how to select the last element from two different classes

for the following html

<div>
    <div class="col1" >
         I dont want to select this
    </div>
    <div class="col2">
         I dont want to select this
    </div>
    <div class="col1">
         I dont want to select this
    </div>
    <div class="col1">
         I dont want to select this
    </div>
    <div class="col2">
         I WANT to select this
    </div>
</div>

How do I select the last element with two different class names?

Tried using

$("col1:last,col2:last)

but it gives back 2 elements

tried

$("col1,col2:last")

and this gives all col1 and last of col2

Upvotes: 2

Views: 2624

Answers (2)

hunter
hunter

Reputation: 63512

try the last() method.

$(".col1,.col2").last();

Upvotes: 1

Guffa
Guffa

Reputation: 700212

First get all elements with any of the class names, then pick the last one:

$('.col1,.col2').filter(':last')

Upvotes: 5

Related Questions