I can't figure out how to add using javascript'a a link that should be attached to the picture. I got the picture, I can't figure out how to add tag a to the picture. thank

var img = new Image(); var div = document.getElementById('left_column'); img.onload = function() { div.appendChild(img); }; img.src ='https://cdn.deliverio.cz/sampure_temp/img/PBA.jpg'; 

    2 answers 2

     var img = new Image(); var link = document.createElement("a"); img.src ='https://cdn.deliverio.cz/sampure_temp/img/PBA.jpg'; link.href= "#"; link.appendChild(img); var div = document.getElementById('left_column'); img.onload = function() { div.appendChild(link); }; 
     <div id="left_column"></div> 

    • A little misunderstood the question? немогу догадаться как добавить к картинке tag a . And the onload asynchronous, no matter before or after the insert method img.src = '' - Vasily Barbashev
    • @ Vasily Barbashev, carefully !!! - HamSter
    • Then I apologize :))) - Vasily Barbashev
    • @ Vasily Barbashev, but in general it’s strange, for some reason I didn’t immediately execute the code when img.src = '' stood after onload - HamSter
    • and what should have been fulfilled? there is nothing to load :) well, this is if empty, and so, the asynchronous code will work 100% after the rest of the code, since has a constant delay, i.e. it is performed not at the time of execution - Vasily Barbashev

     var img = new Image(); var div = document.getElementById('left_column'); img.onload = function() { var link = document.createElement('a'); link.href = this.src; link.appendChild(img); div.appendChild(link); }; img.src ='https://cdn.deliverio.cz/sampure_temp/img/PBA.jpg'; 
     <div id="left_column"></div>