santa
santa

Reputation: 12512

Target child with jQuery

I have the following code:

<div class="topLevel">
    <span class="misc pointDown"></span>
<span class="misc"></span>
<span class="b">item</span>
</div>

When I click on the topLevel I need to replace "pointDown" with "pointUp". I think I have difficulty targeting the right element.

$(".topLevel").live('click', function() {
    $(this).next("span").removeClass("pointDown").addClass("pointUp");
});

Upvotes: 2

Views: 5735

Answers (4)

DNR
DNR

Reputation: 986

Don't assume the first item is your element - it's not the proper answer (if you ask me).

$('.topLevel').click(function() {
     $(this).find('.misc.pointDown').removeClass('pointDown').addClass('pointUp');
}

Upvotes: 0

mattsven
mattsven

Reputation: 23253

$(".topLevel").live('click', function() {
   $(this).find("span:eq(0)").removeClass("pointDown").addClass("pointUp");
});

Upvotes: 0

Brandon
Brandon

Reputation: 39182

$('.topLevel').live('click', function() {
    $(this).children('span.pointDown').removeClass("pointDown").addClass("pointUp"); });

Upvotes: 0

Naftali
Naftali

Reputation: 146302

try this:

$(".topLevel").live('click', function() {
    $(this).children('.misc').first()
           .removeClass("pointDown").addClass("pointUp");
});

Upvotes: 4

Related Questions