Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  57] [ 3]  / answers: 1 / hits: 25541  / 12 Years ago, sat, january 12, 2013, 12:00:00
<div id=mydiv>
<img src=myimage.jpg />
</div>
<script language=javascript>
var a=document.createElement('a');
a.href='http://mylink.com';
document.getElementById('mydiv').appendChild(a);
</script>


The script doesn't work to create link on image



<div id=mydiv>
<a href=http://mylink.com><img src=myimage.jpg /></a>
</div>

More From » image

 Answers
19

You are putting the link after the image.



You need to move the image so it is inside the link.



var image = document.getElementById('mydiv').getElementsByTagName('img')[0];
a.appendChild(image);

[#80918] Friday, January 11, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
diamondlauryna

Total Points: 386
Total Questions: 93
Total Answers: 103

Location: South Korea
Member since Fri, Sep 11, 2020
4 Years ago
diamondlauryna questions
;