Wednesday, 11 September 2013

How to get first child with specify element in JQuery?

How to get first child with specify element in JQuery?

I have following HTML..
<div class = "thumbnail">
<img src = "somePath">
</div>
<div class = "thumbnail">
<img src = "somePath">
</div>
<div class = "thumbnail">
<img src = "somePath">
</div>
<div class = "thumbnail">
<a href = "someLink"><img src = "somePath"></a>
</div>
<div class = "thumbnail">
<a href = "someLink"><img src = "somePath"></a>
</div>
I want to iterate img tags under the first child of div.thumbnail. I want
to filter them out. I use as..
var i =0;
$("div.thumbnail :first-child").each(function() {
console.log("Touch "+(++i)+" time(s) !");
if($(this).context.nodeName.toLowerCase() == 'img') {
console.log("Getting Image Tag.");
}
});
My question is I want to filter in getting elements ..
$("div.thumbnail :first-child :img").each(function(){});
I don't want to iterate as count of div.thumbnail . I want to iterate as
count of div.thumbnail with first child tag is img. How to figure it out ?

No comments:

Post a Comment