View Full Version : Help With displaying an Image in HTML

07-10-2011, 04:13 PM

I'm having problems displaying images on a site. Basically I have a to change an image each time a button is pressed on a piece of hardware.

This is the code to detect a button press

// Check Button
tstileo = new Image();
tstilec = new Image();
tstileo.src = '/images/1.jpg';
tstilec.src = '/images/2.jpg';

document.getElementById('btn0').innerHTML = (getXMLValue(xmlData, 'btn0') == 'up') ? document.Image=(tstileo): document.Image=(tstilec);

So when the button is pressed/unpressed it should display tstileo and tstilec

What format should my HTML code be to display the image ?

This is whats there at the moment

<span id="btn0">?</span></p>

When running all I get is text saying
[OBJECT HTMLImageElement]
This site is being hosted on a microchip, btn0 is a press button switch on the board. The idea is to use this as a remote monitoring system. It runs Javascript XML HTML and CSS.

Any help would be greatly appreciated, I have spent hours searching for a way to do this ( simple ? ) task but haven't got any further than this. :confused:



07-10-2011, 07:34 PM
Unless you really want to preload those images, just do this:

// Check Button
document.getElementById('btn0').innerHTML = '<img src="/images/'+(getXMLValue(xmlData, 'btn0') == 'up' ? '1' : '2')+'.jpg">';

07-10-2011, 08:08 PM
Thank you so much :thumbsup:

That works perfectly

I've been having to get to grips with Javascript / CSS / XML and C to do this so its a bit of a struggle ... fun though !!

I really appreciate your help and time



07-29-2011, 08:32 PM

I have another similar question/problem.

I want to modify a line of HTML like above using a variable. The idea is to turn my voltage reading red if it drops below 22 or above 26 volts. I'm using this:

document.getElementById('volstat0').innerHTML= '<div class = "'+getXMLValue(xmlData, 'volstat0')+'">';

This idea is if the voltage is ok the variable 'volok' is sent telling page to use black color or 'volprob' which tells it to display in red.

I have checked that 'volstat0' changes from 'volok' to 'volprob' when the voltage does so I presume my problem lies in the way I'm writing the Java ?

Can anyone help please ? :)

Edit: Solved it at last,

Used this instead:

document.getElementById('volstat0').style.color = (getXMLValue(xmlData, 'volstat0') == '1') ? '#F00' : '#090';