Reputation: 5644
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
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
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