allegutta
allegutta

Reputation: 5644

Combine the 'this' selector?

In jQuery I have selected a html class, and using 'this' to reference it within the function. How can I select an img within that class?

The code under did not work:

HTML:

<div class="test">
    <img src="batman.png">
</div>

jQuery:

$(".test").mouseover(function() { 
    console.log($(this img).attr("src"));
});

Upvotes: 0

Views: 198

Answers (3)

MegaMind
MegaMind

Reputation: 671

You will have to use a selector.

$('img', this)

Upvotes: 0

Satpal
Satpal

Reputation: 133403

You need to use $.fn.find() method

$(this).find('img') 

OR, use jQuery(selector ,context), internally it will be convert to find()

context: A DOM Element, Document, or jQuery to use as context

$('img', this)

Upvotes: 3

Paul Wellner Bou
Paul Wellner Bou

Reputation: 554

I am not sure, what exactly you are trying to achieve, but maybe you are trying to do this?

$(this).find('img')

See https://api.jquery.com/find/.

Upvotes: 2

Related Questions