-3
<html>
<head></head>
<body> 
<script>
    var image1 = newImage();
    image1.src = "./SkinnersLogo.png";
    document.getElementById("d").innerHTML = image1;
</script>
<img id="d"> </img>
</body>
</html>

I have set the value of image 1, to a source. I have then tried to innerHTML this image, and then display it within an img tag. However, all I see is a blank screen on the HTML window. Does anyone know what I am doing wrong here? I cannot spot any mistakes, thanks.

2 Answers2

4
  1. You forgot the space between the new operator and the Image function name
  2. You called getElementById before the element existed
  3. You tried to assign a DOM node (the return value of new Image) as if it were a string of HTML (which it isn't). Use appendChild(your_node) not innerHTML = your_html.

Learn how to use the developer tools in your browser. You would have received a number of error messages in the Console which could have helped you debug this yourself.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

If the image tag is already there on the page you could simply assign the src attribute like below.

<img id="d"> </img>

<script>
    var source= "./SkinnersLogo.png";
    document.getElementById("d").src = source;
</script> 
ConsoleLog
  • 501
  • 3
  • 12