2

I have a folder with images called " LaptopsImages ".

In my file called laptops.php i have an array list with different laptops with variables like: title, brand, price, description, spec and pictures names. My array list is convert to XML DOM tree.

On my file called product.html i wrote a code that take every product data into the variables and display it:

var title = $theProduct.find('title').text();
var brand = $theProduct.find('brand').text();
var price = $theProduct.find('price').text();
var description = $theProduct.find('description').text();
var spec = $theProduct.find('spec');
var imageFilename = $theProduct.find('picture').text();

document.getElementById("title").innerHTML=title
document.getElementById("brand").innerHTML=brand
document.getElementById("price").innerHTML=price                        
document.getElementById("description").innerHTML=description
document.getElementById("spec").innerHTML=description

I have accomplished to display every product data into my html except the pictures for each laptop. Is any way that i can display pictures with the name they have already from my variable? Assume that the pictures have names " 0001.jpg , 0002.jpg " and so on.

Every picture can be taken from: var imageFilename=$theProduct.find('picture').text();

4 Answers 4

1
document.getElementById("picture").src = imageFilename

if there's a <img> tag with the id picture

Sign up to request clarification or add additional context in comments.

3 Comments

I have done this and i have one more problem now. My local server is trying to find the pictures but they are in a sub folder. Is any way that i can specify the way to look throw this folder? The folder is called "LaptopsImages". Also when i check from console i saw this: GET localhost:8080/0001.jpg 404 (Not Found)
That's a problem with the method you're using to serve the images, and not the HTML or CSS or JS
No worries. I just found the solution. Thanks for the advice.
1

If you use jquery you can do :

$("#picture").attr("src", imageFilename);

Comments

0
document.getElementById('myImage').src = imageFileName;

If you need the name from the ImageFileName then you can use:

name= imageFileName.substring(0, imageFileName.length - 4);/*to chop off .JPG*/

With Jquery:

$("#myImage").attr("src", imageFilename);

Comments

0
document.getElementById("price").innerHTML= "<img src=\"" + imageurl+ "\">";

You can use this code to display a complete image in inner div where div id is price

Comments

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.