views:

133423

answers:

5

I'd like to use a selector to select the child img of the div I'm clicking on this example:

<div id="..."><img src="..."></div>

To get the div, I've got this selector:

$(this)

How do I get the img with a selector?

+2  A: 

It's been some time, but have you tried $(this).children()[0] ?

that would work although it returns a dom element not a jq object
redsquare
I don't know if it is the best approach to the current situation, but if you want to go this route and still end up with a jQuery object, just wrap it that way: `$($(this).children()[0])`.
patridge
+2  A: 

Without knowing the ID of the DIV I think you could select the IMG like this:

$("#"+$(this).attr("id")+" img:first")
Adam
this probably actually works but it's kinda the Rube Goldberg answer :)
Scott Evernden
+18  A: 

Thanks maxam, I've found it with your start :)

jQuery(this).children("img")

Alex
+68  A: 

You could also use

$(this).find('img');

which would return all imgs that are descendants of the div

Phil
+207  A: 

The jQuery constructor accepts a 2nd parameter which can be used to override the context of the selection.

jQuery("img", this);
Simon
+1 Thanks, didn't know about this
Andreas Grech
for what it's worth: jQuery("img", this)is internally converted into: jQuery(this).find("img")So the second is ever so slightly faster. :)
Paul Irish
Thanks @Paul, I was worried that using find() was *wrong*, turns out it's the only way ;)
Agos
Thanks, Simon. Big help.
j33r