views:

225

answers:

2

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

+1  A: 

try the last() method.

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

hunter
but I want the last div that has one of the two class names(not one)
Yasir Laghari
I see what you mean, how about that?
hunter
is last a jquery function?
Yasir Laghari
btw Guffa's reply fixed it
Yasir Laghari
jquery seems to think last() is a method
hunter
nice, learned something new. last() is exactly what I'm looking for. In my case it won't help though since I'm using 1.3. Thanks a lot Hunter for your time and effort!
Yasir Laghari
`$('.col1, .col2').slice(-1).eq(0)`. But it'd be better to upgrade to 1.4 if you can. `last()` as a method (or this alternative) is somewhat more efficient than filtering, and considerably more efficient than including a non-standard Sizzle-only selector such as `:last` in the query itself.
bobince
+4  A: 

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

$('.col1,.col2').filter(':last')
Guffa
That worked, thanks Guffa!
Yasir Laghari