Jump to content

Change Background Image Css Property With Javascript


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

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 + ")";}

Edited by Ingolme
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...