0

Possible Duplicate:
javascript - document.write error?

I have a piece of Javascript that I'm using that checks to see if a div is a certain style and then runs an if else statement:

window.onload = function () {
    if (document.getElementById('slidingDiv').style.display == 'none') {
        document.write('<a href="#show" class="show_hide"><img src="images/show-more-arrow.jpg" width="61" height="45"></a>');
    } else {
        document.write('<a href="#show" class="show_hide"><img src="images/show-less-arrow.jpg" width="61" height="45"></a>');
    }
};

This works OK, but it only shows this HTML image on the screen. I need it to add the HTML to the existing code on the page.

Many thanks

Community
  • 1
  • 1
Pete Naylor
  • 756
  • 2
  • 13
  • 33

2 Answers2

7

Use appendChild or innerHTML

example:

document.getElementById("elemId").innerHTML = '<a href="#show" class="show_hide"><img src="images/show-less-arrow.jpg" width="61" height="45"></a>'
ke20
  • 615
  • 4
  • 18
2
var wrapper = document.createElement('div');
var image = document.createElement('img');
image.src='images/show-more-arrow.jpg';
wrapper.appendChild(image);

Something like this.