Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Friday, May 25, 2012
$(this) selector and children?
I'd like to use a selector to select the child img of the div I'm clicking on this example:
The jQuery constructor accepts a 2nd parameter which can be used to override the context of the selection.
ReplyDeletejQuery("img", this);
Which is the same as
jQuery(this).find("img");
If the imgs are direct descendants of the clicked element, you can also use:
jQuery(this).children("img");
You could also use
ReplyDelete$(this).find('img');
which would return all imgs that are descendants of the div
If you need to get the first img that's down exactly one level, you can do
ReplyDelete$(this).children("img:first")
If your DIV tag is immediately followed by the IMG tag, you can also use:
ReplyDelete$(this).next();
Without knowing the ID of the DIV I think you could select the IMG like this:
ReplyDelete$("#"+$(this).attr("id")+" img:first")
Try this code:
ReplyDelete$(this).children()[0]