Jump to content

Change Background Image Css Property With Javascript


madsovenielsen

Recommended Posts

Hey. i just want to know if this is a 100% valid JavaScript.

function changeImage() {newImage = "url(test2.png)";document.getElementById('image').style.backgroundImage = newImage;}

Its Working fine. Is it the simplest way of doing it ?/mads

Link to comment
Share on other sites

Hey. i just want to know if this is a 100% valid JavaScript.
function changeImage() {newImage = "url(test2.png)";document.getElementById('image').style.backgroundImage = newImage;}

Its Working fine. Is it the simplest way of doing it ?/mads

It is completely valid.You could make your function more flexible by allowing the programmer to set the new image URL in the function parameter:
function changeImage(url) {  document.getElementById('image').style.backgroundImage = "url(" + url + ")";}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...