Ryan Saxe
Ryan Saxe

Reputation: 17829

specific element in `this` with jquery

So in jquery, if I want to target a div in the .test class on a click I use the following code:

$('.test div').click(function(){
    //do something
});

But if the "do something" part requires a this parameter, I can't seem to get the same effect. So lets say I want to make all bold text in any div with class test fade out. I would have to do something like this:

$('.test div').click(function(){
    $(this ' b').animate({
        opacity:toggle
    },500);
});

But that is invalid code. So How would I target a specific element in a this Jquery selector?

Upvotes: 0

Views: 66

Answers (2)

Swarna Latha
Swarna Latha

Reputation: 1004

Below code will help you to do this

 $(".test").click(function () {
                $("b", this).fadeToggle(500);
 });

Take a look at this jqfaq.com site,it has more jquery related faqs. It may be helpful to you.

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388316

You need to pass the context as the second parameter to jQuery

 $('b', this).animate({
     opacity:toggle
 },500)

Another solution is to use .find()

 $(this).find('b').animate({
     opacity:toggle
 },500)

Upvotes: 6

Related Questions