Pitch Rock Pitch Rock - 1 year ago 65
jQuery Question

Add link to image dynamically

If i have "img" element id = "myimg".

Is posible to add link to "img" without edit html page using jQuery

<img id="myimg" src="image.png">

I like to make "myimg" have link like this.

<a href="test.html"><img id="myimg" src="image.png"></a>

Answer Source

You can use wrap():

$("#myimg").wrap("<a href='test.html'></a>');


$("#myimg").wrap($("<a>").attr("href", "test.html"));


var a = $("<a>").attr("href", "test.html");