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>');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With