Emloy
Emloy

Reputation: 25

Javascript Image/Text Object

I was trying to give an object an Image but I doesn`t give it back . What did I wrong ? ^^

var bank = {
    money:"$",
    currency:"Dollar",
    sum:50,
    src: "Logo.png"
};      

document.getElementById("test").innerHTML=  " Your amount is" + bank.sum + "  "+  bank.money + "("+ bank.currency+")" + bank.src;       

Upvotes: 0

Views: 599

Answers (4)

Emloy
Emloy

Reputation: 25

Is it possible to box an object like this ?

var screenX1 = {
    image:"image001.png", 
    links: {
        link {
            area:[45, 143, 106, 158];
            reference to screenx2
        }
        link {
            area:[45, 143, 106, 158];
            reference to screenx3
        }
    }, 
}; 

var screenx2 = {
    ......

Upvotes: 0

user3127202
user3127202

Reputation: 29

Try adding the image in an <img> element like this:

document.getElementById("test").innerHTML=  " Your amount is" + bank.sum + "  "+  bank.money + "("+ bank.currency+")" + " `<img src= '"+bank.src+"' />` "; 

Upvotes: 0

Filip Kov&#225;č
Filip Kov&#225;č

Reputation: 579

Add img tag for show image like below.

document.getElementById("test").innerHTML="Your amount is " + bank.sum + "  "+  bank.money + " ("+ bank.currency+")" + "<img src='"+bank.src+"' />"; 

Upvotes: -1

Dylan Meeus
Dylan Meeus

Reputation: 5802

You are not using an image anywhere as far as I can tell. You can't just give an "image" to javascript. You can store the URL to an image in javascript, and then you can assign this to an HTML <img>tag.

In html the tag would look like <img id="testimage" src="Logo.png/>. Then you could use javascript to change the image source. You could do it like

 var image = document.getElemenyById("testimage");
 image.src = bank.src;

Or, if you want to add an image to the body completely in Javascript, you could do that as well.

as you have commented on your answer, I guess this is the way you actually want to do it

var img = document.createElement("img"); // creates <img>
img.src = bank.src; // sets the source to the source of the 'bank'
document.getElementsByTagName("body")[0].appendChild(img); // adds it to the body

You could of course add it to your (div?) which you are using the your question.

 document.getElementById("test").appendChild(img);

Upvotes: 2

Related Questions