JQuery add tag <a> around <img> – Education Career Blog

How can I add tag aroud with link to that image with JQuery?

,

This will wrap a set of images with links to them:

$('some selector for the images').each(function() {
    $(this).wrap("<a href='" + this.src + "'/>");
});

…uses .each (link), .wrap (link), and the native DOM src (link) property for image elements.

Edit Or as Pointy points out (but not pointedly), just pass a function into wrap:

$('some selector for the images').wrap(function() {
  return "<a href='" + this.src + "'/>";
});

Live example

,

$('#img').each(function(){
    var $this = $(this); 
    $this.wrap('<a href="' + $this.attr('src') + '"></a>');
});

Leave a Comment