Hi,
I have an element on a page with background image set using css:
// CSS snippet
.mystyle {
background-image: url(some_image.gif);
// and other styles like width, height, padding, text-align, etc.
}
// HTML snippet
<input type="button" name="mybutton" id="b1" class="mystyle" onclick="
some_function();" value="Apply">
In a Javascript function, I'm trying to do the following:
newImage = "url(new_image.gif)";
document.getElementById( "b1" ).style.backgroundImage = newImage;
This does not work and the old background image disappears.
Moreover, when I check (using alerts) what the backgroundImage property was set to before and after I changed it:
newImage = "url(new_image.gif)";
alert( document.getElementById( "b1" ).style.backgroundImage );
document.getElementById( "b1" ).style.backgroundImage = newImage;
alert( document.getElementById( "b1" ).style.backgroundImage );
The first alert shows blank (empty string) and the second alert shows the newImage url.
So there is definitely something weird going on here. I'm just not sure what. Can anyone help?
EDIT: If, however, I only change the backgroundColor attribute and set the backgroundImage to 'none', the change seems to work just fine (meaning, the backgroundImage disappears as before and i can see the new backgroundColor.