dom – Reference either of 2 images nested inside a <div> using jQuery – Education Career Blog

My DOM looks like:

<div id="blah-1">
  <div class="class1">
     <div class="class11>
        <a href=""><img src=""></a> <b>blah</b>
        <a href=""><img src=""></a>
     </div>
  </div>
</div>

I have to change the source of the 1st or sometimes 2nd img.

I am using jQuery and don’t have a strong handle with selectors just yet!

,

You can use :eq(n) to select an element at a given position:

$("#blah-1 .class1 .class11 img:eq(0)").attr("src", newSource);
$("#blah-1 .class1 .class11 img:eq(1)").attr("src", newSource);

Oh, and you’re missing a closing ” after class11, not sure if that’s pasted code or not.

Leave a Comment