0

this let's me load a picture to my html tag. I would like to add a button and input text box in my html so that i can change the image by putting url. I tried to use put the onclick function in "head" but failed.

// what i tried
document.getElementById('url').onclick = function() {
img.src=document.getElementById('picUrl').value;
}​

//what i have
<head>

var img=new Image();
img.onload=function(){
    ctx.drawImage(img,0,0);
}
img.src="xxxx";
}
</head>
  • because the image element has not been rendered. – epascarello Aug 27 '17 at 01:34
  • https://stackoverflow.com/questions/8739605/getelementbyid-returns-null – epascarello Aug 27 '17 at 01:34
  • can you please explain a little more detail? what i want to do it kind of have the onload function to be a default img in my html and let user to enter url and change the img latter. what i am facing is the whole div where i place my img turns to be white and i can't even load the default img – Yang Tang Aug 27 '17 at 01:49
  • You referenced an element before it exists, it is like someone calling your name before you enter into a room. You need to use window.onload to add the event or document ready. – epascarello Aug 27 '17 at 01:51

0 Answers0