Jcfg.J Jcfg.J - 1 month ago 9
HTML Question

Get Sibling Element of an Image

So I have

<a href="1.html">
<img src = "image.jpg" class = "picture"/>
<div class="desc"><p>Brief Description</p></div>
</a>

<a href="2.html">
<img src = "image2.jpg" class = "picture"/>
<div class="desc"><p>Brief Description</p></div>
</a>


How do I cycle through the pictures and alter the div accordingly?

So far I have something like:

var pictures = $('.picture');
(var a = 0; a < pictures.size(); a++){
var description = (pictures.get(a)) (.siblings?)(.next?);
//Do what I want with the description
}

Answer

You can use Jquery .each() to loop through the elements. And .siblings() to get the sibling of that element.

$('.picture').each(function(){ var description = $(this).siblings('div. desc').text(); // Do what I want with the description });

Comments