Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript JQuery – Using Image Attr to populate ‘caption’ paragraph Re: JQuery – Using Image Attr to populate ‘caption’ paragraph

#94068
Eamonn
Member

Solved via StackOverflow:

$("#yourdiv img").each(function() { //Loops through the images in a specific div. $(this) represents the current image in the loop
$(this).addClass("yourclass"); //Add a class
$(this).wrap("

"); //Add a wrapping div
$("

").text($(this).prop("title")).insertAfter($(this)); //Create a new paragraph with the title property of the image as text and put it after the image (inside our newly created div)
});

I’ve included this in the Snippets gallery