Duccio B.
Duccio B.

Reputation: 77

JavaScript image in div

I have this code:

function getImage(imgPath,override){
    image=new Image();
    image.src=imgPath;
    document.getElementById('imgboxDavanti').appendChild(image);
}

and the HTML that calls this function is:

<a href='#' onclick="getImage('immagini/giaccasaccobacino/1petto/beige.png','true')">

this is the div where i would put the image

<div id="imgbox">
    <div id="imgboxDavanti">IMAGE HERE</div>
    <div id="imgboxDietro"></div>
</div>

but I don't display the image (and if I pass a wrong path as an argument, I see the typical "image not found" icon). What's the problem?

Upvotes: 0

Views: 79

Answers (2)

user1693593
user1693593

Reputation:

And if you modify to this? (there is no obvious error in your code, but check mainly what error message says if any..);

function getImage(imgPath,override){
    image = document.createElement('img');
    image.onload = function() {
        document.getElementById('imgboxDavanti').appendChild(image);
    }
    image.onerror = function(e) {
        console.log(e);
        alert('Error occurred, see console for details.');
    }
    image.src = imgPath;
}

Upvotes: 1

Mirko Cianfarani
Mirko Cianfarani

Reputation: 2103

Try with create element for <input type="image" src="picture.gif" />

function getImage(imgPath,override){
image=document.createElement('INPUT');
image.type="image";
image.src=imgPath;
document.getElementById('imgboxDavanti').appendChild(image);
}

DEMO

Upvotes: 1

Related Questions