I need to get the text inside each 'dd' and place it as a value of the data-description attribute of the related 'a.myClass'. It would be perfect if the data-description attribute is created only if 'gallery-caption' is present.
HTML
<dl>
<dt>
<a class="myClass"></a>
</dt>
<dd class="gallery-caption">This is my caption</dd>
</dl>
CURRENT JS (non-working)
$(".myClass").each(function(){
var Caption = $(this).find('dd.gallery-caption').text();
$(this).attr('data-description', Caption );
});
Thanks for your help
FINAL WORKING SOLUTION
$(".myClass").each(function(){
var Caption = $(this).parent().next('dd.gallery-caption').text();
if (Caption && Caption !== ''){
$(this).attr('data-description', Caption );
}
});