Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  15] [ 3]  / answers: 1 / hits: 18193  / 10 Years ago, fri, december 19, 2014, 12:00:00

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


More From » image

 Answers
5

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>

[#68445] Tuesday, December 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jadyngraysons

Total Points: 455
Total Questions: 109
Total Answers: 98

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
jadyngraysons questions
Thu, Apr 23, 20, 00:00, 4 Years ago
Sat, Jan 18, 20, 00:00, 4 Years ago
Tue, Dec 31, 19, 00:00, 4 Years ago
;