Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am new to coding. I cannot figure out why these images will not load on the webpage. I have made sure that they are listed the same as they are saved.

What I have tried:



<title>Car Search



Select a Car

 
Click to Search!











var carID=[0,1,2,3,4];

var names=[
"Mercedes",
"VolksWagen",
"Toyota",
"Subaru",
"Audi"
];

var colors=[
"Navy Blue",
"Crimson",
"Lollipop Green",
"Sunset Yellow",
"Blood Red"
];

var price=[
"200,000 Dollars",
"40,000 Dollars",
"10,000 Dollars",
"25,000 Dollars",
"150,000 Dollars"
];
var images=[
"mercedes.jpg",
"volkswagen.webp",
"toyota.jpg",
"subaru.jpg",
"audi.jpg"
];
var selection=document.getElementById("cars");
//populate the dropdown dynamically
for (var i = 0; i &lt; names.length; i++) {
var option=document.createElement("option");
option.setAttribute("id",carID[i]);
option.setAttribute("value",names[i]);
option.innerHTML=names[i];
selection.appendChild(option);
};

document.getElementById("search-car-btn").onclick=function(e){
var select=document.getElementById("cars");
var option=select.options[select.selectedIndex];

var id=option.getAttribute("id");

document.getElementById("car-name").innerHTML=names[option.id];
document.getElementById("car-color").innerHTML=colors[option.id];
document.getElementById("car-price").innerHTML=price[option.id];
document.getElementById("car-image").setAttribute("src","images/"+images[option.id]);
}
Posted
Updated 1-Dec-17 10:35am

1 solution

You are not following up the training I assume,
var images=[
"mercedes.jpg",
"volkswagen.webp",
"toyota.jpg",
"subaru.jpg",
"audi.jpg"
];
Does not mean that they are available on the machine. To make sure, that they load, you need to put them in the directory "images", which is a subdirectory of the directory where your HTML file is.

/
|- index.html
|- images/
   |- mercedes.jpg
   |- volkswagen.webp
// so on

This is the way it should happen, otherwise the relevancy would break and you will not be able to show the images.

One more thing, when this happens ever, just right-click and open the resource in a new page. That shows the URL which is being built on the runtime. That URL can give you an idea of where the browser is looking for the resource. That way you can modify and target it to the location where your resources actually are.
 
Share this answer
 
Comments
Member 13552519 1-Dec-17 16:43pm    
so I understand how it needs to be set up, but what does the code need to start and end with? the slashes?
Afzaal Ahmad Zeeshan 1-Dec-17 16:56pm    
Yes, the slashes denote the root (/images/myphoto.png) means, the images folder in the root directory and the myphoto.png image to be rendered. Mostly, ~/ is also used, but that is totally debatable.
Member 13552519 1-Dec-17 17:16pm    
when I added this it made the rest of my content associated with the button not work... I think I am doing something wrong.. :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900