Artur Caraus
Artur Caraus

Reputation: 3

jquery :last-child did not work properly

jQuery $("div span[z*='w']:last-child").css({color: "red"});

and the html

<div>
   <span z="ww">Sam</span>  <!-- Didn't works -->
   <span>Sam 2</span>
</div>
<div>
    <span z="ww">Glen,</span>
    <span z="ww">David</span> <!-- Works -->
</div>

How to select the span from the first div?

Live example http://jsbin.com/izubaj/edit#javascript,html,live

Upvotes: 0

Views: 319

Answers (3)

pete
pete

Reputation: 25081

I may be misunderstanding the question, but I think you want the span that has "Sam 2" as well as "David".

<div>
    <span z="ww">Sam</span>  <!-- Didn't works -->
    <span>Sam 2</span>
</div>

If that's the case, try:

$('div span[z*="w"]').siblings(':last-child').css({color: 'red'});

Upvotes: 0

Jan Beck
Jan Beck

Reputation: 1000

Try this:

$("span[z*='w']:last", 'div').css({color: "red"});

Upvotes: 1

Rich O&#39;Kelly
Rich O&#39;Kelly

Reputation: 41757

Your selector actually chooses the span elements and then applies the last child pseudo selector to that collection, try the following:

$("div").find('span[z*="w"]:last').css({color: "red"});

See here for update.

Upvotes: 1

Related Questions