jquery - Override/Inset new alt tag from <figcaption> tag -


i have piece of jquery, see below, wraps images in href tag can used in lightbox.

i need adjust appears in alt tag well, , needs generated whatever user puts in caption box in ckeditor (which generates figcaption tag under each image). need able insert data empty alt="" , able override if there in alt tag.

the html being output @ moment is:

<figure class="image">     <a data-imagelightbox="f" href="/ckfinder/userfiles/files/screenshot_south-downs-lines-rsc_51_23330-1_22091_10-00-48.jpg">         <img width="1600" height="900" src="/ckfinder/userfiles/files/screenshot_south-downs-lines-rsc_51_23330-1_22091_10-00-48.jpg" alt="caption 1"></img>     </a>     <figcaption>         caption     </figcaption> </figure> 

the current script have follows:

$('#article-copy img').each( function() {     var $img = $(this),         alt = $img.next('figcaption').text(),         href = $img.attr('src');     $img.wrap('<a href="' + href + '" data-imagelightbox="f"></a>'); }); 

thank help

if understand correctly, want change alt attribute of image. should work:

$('#article-copy img').each( function() {     var $img = $(this),         alt = $img.next('figcaption').text(),         href = $img.attr('src');     $img.attr('alt', alt );     $img.wrap('<a href="' + href + '" data-imagelightbox="f"></a>'); }); 

Comments