Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 848
wearetherock Avatar asked Nov 29 '22 19:11

wearetherock


1 Answers

You can use wrap():

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

or

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

or:

var a = $("<a>").attr("href", "test.html");
$("#myimg").wrap(a);
like image 198
cletus Avatar answered Dec 05 '22 17:12

cletus