Question

So I am trying to make a JavaScript program that will take a URL for an image and then put it onto the page while creating an <img> tag so that I can just continue pasting as many photos as I want. Here's the code:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Low-Budget Online Album</title>
<meta charset="utf-8">
<script>
    function init() {
        var button = document.getElementById("addButton");
        button.onclick = buttonClick;
    }
window.onload = init;

    function buttonClick() {
        var imageSource = document.getElementById("imageInput").value;
        if (imageSource == "") {
            alert("Please enter the source for an image.");
        }
        else {
            var newImage = document.createElement("img");
            var newSrc = document.getElementById("newImage").src= imageSource;
    
            imageInput.value = "";
        }
    }       
</script>
</head>
<body>
<input type="text" id="imageInput" size="40" placeholder="Image Source">
<input type="button" id="addButton" value="Add Image">
<img id="images" src="">
</img>
</body>
</html>

My problem is, is that when I put int a URL (or picture src from my PC) it says that TypeError: document.getElementById(...) is null, and points to line 20, being my

var newSrc = document.getElementById("newImage").src= imageSource;

line. Any ideas?

Was it helpful?

Solution 2

do you mean something like this:

function init() {    
    var button = document.getElementById("addButton");
    button.onclick = buttonClick;    
}
window.onload = init;

function buttonClick() {        
    var imageSource = document.getElementById("imageInput").value;
    if (imageSource == "") {
        alert("Please enter the source for an image.");
    }
    else {
        var newImage = document.createElement("img");
        newImage.src= imageSource;
        newImage.setAttribute("id", "newImage");
        imageInput.value = "";
        document.body.appendChild(newImage);
    }
} 

Demo:: jsFiddle

OTHER TIPS

Use this

else {
        var newImage = document.createElement("img"); //this line creates element <img> element in the dom.
        newImage.setAttribute("id", "newImage");             
        newImage.src= imageSource;
        document.body.appendChild(newImage);//adds element <img src="a.jpg" id='newImage'>to the dom.
        imageInput.value = "";
    }

Understand what mistake you have done above:

1.First you created element and assign to a variable newImage

 var newImage=document.createElement("img");      

2.You are calling

 document.getElementById('newImage');

Here newImage as element that you created and in the dom there is no element with id as newImage so you were getting null.

var newImage = document.createElement("img");
var newSrc = document.getElementById("newImage").src= imageSource;

Become:

var newImage = document.createElement("img");
newImage.setAttribute('id','newImage');
var newSrc = document.getElementById("newImage").src = imageSource;

in mdn you can see that createElement only create element and not add it to DOM. So if you want add created element you need change your code like this

var newImage = document.createElement("img");
newImage.id = "newImage";
document.body.appendChild(newImage);

after this line will work

var newSrc = document.getElementById("newImage").src= imageSource;

but you don't need get find it if you already have this image in newImage variable, so this line you can change like

var newSrc = newImage.src= imageSource;

UPDATE
Possibly you need use className instead of id because id should be unique on page, but as i understand you want add many images

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top