2

Currently working on Loading image dynamic. If the custom image has the path it has to take the custom image not the default one if there is no custom image path are not available it has to load default image in the given ID.

As I am new to Javascript I am trying my below code.

var customImageurl = "custom_Image/web.png";
var defaultImageurl = "";
    function loadGraphics(){
        document.getElementById("loadImage").src = defaultImageurl;
    }
loadGraphics();
<div id="loadImage"></div>

It was not working kindly help me.

Thanks in advance.

Regards Mahadevan

4
  • show your html pls i want to check the img tag Commented Dec 19, 2014 at 0:46
  • You need to program your logic using ifs like in your description. Commented Dec 19, 2014 at 0:46
  • hi thanks @PM 77-1 can you please help me Commented Dec 19, 2014 at 0:49
  • What exactly do you need help with? Commented Dec 19, 2014 at 0:51

2 Answers 2

3

Your problem is that a div has no src attribute, as in your example you have <div id="loadImage"></div>

Your defaultImageurl is also empty.

The solution is to use an <img/> element:

var customImageurl = "custom_Image/web.png";
var defaultImageurl = "";
function loadGraphics(){
    //v-- will not work! 
    document.getElementById("loadImage").src = defaultImageurl;
    //v-- will work given your example conditions
    document.getElementById("loadImage").src = customImageurl;
}
window.onload = function(){
    loadGraphics();
};
<div id="loadImage-wrap">
    <img id="loadImage" />
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

You are trying to use a src attribute on a <div> element.

3 Comments

hi if i give innerhtml for the div also the image was not working
yes @user3292788 I am new to javascript i am trying something and I have changed src to innerhtml still it was not working kindly help mer
The only way to associate an image to a <div> is to go through the CSS property background. But the semantic will be different. If you want to insert an image in your <div> you have to create it first with document.createElement or just with the object Image. Take a look at this exemple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.