Parijat Kalia
Parijat Kalia

Reputation: 5105

Selecting an img element, without an id, stacked in multiple div's

I have an img, that does not have an id and is stacked something like this:

<div class = "someclass">
  <div>
   <div>
     <img>
   </div>
  </div>
</div>

I am using the following jQuery selector to get the result :

$('.someclass div div img')

did not work, I also tried

$('.someclass').children('img')

that did not work either, and then I tried this ridiculous selector:

$('.someclass').children('div').children('div').children('img') ;

and that offcourse did not work.

How do I select that elusive img element, and where can I read up more in detail?

Upvotes: 0

Views: 175

Answers (3)

Crsr
Crsr

Reputation: 624

You can use children selector

$('.someclass').children(0).children(0).children(0);

Upvotes: -2

Guffa
Guffa

Reputation: 700910

If it's the only image element in that div, then you can just look for the img element somewhere in the element with that class:

var image = $('.someclass img');

Upvotes: 0

Jeff Miller
Jeff Miller

Reputation: 2443

Try using jQuery's find() method:

$(".someclass").find("img");

Upvotes: 6

Related Questions