I found a lot of information on how to change the background image of a div using JavaScript, but I'm trying to use JavaScript to determine which background image is displayed. The code for setting the image is as follows:
document.getElementById("widgetField").style.background="url(includes/images/datepicker_open.png)";
I tried every combination that I can come up with to access the url of the background image, but still no dice:
alert(document.getElementById("widgetField").style.backgroundImage.url); - returns Undefined alert(document.getElementById("widgetField").style.backgroundImage); - empty response alert(document.getElementById("widgetField").style.background); alert(document.getElementById("widgetField").style.background.image); alert(document.getElementById("widgetField").style.background.url); alert(document.getElementById("widgetField").style.background.image.url); alert(document.getElementById("widgetField").style.background.value); alert(document.getElementById("widgetField").style.background.image.value); alert(document.getElementById("widgetField").style.background.image.value); alert(document.getElementById("widgetField").style.backgroundImage.value);
Does anyone know how to do this? Is it possible?
By the way, here is how the image is set in CSS at the beginning:
#widgetField { width: 290px; height: 26px; background: url(../images/datepicker_closed.png); overflow: hidden; position: relative; }
UPDATE:
If I run the following, it works:
document.getElementById("widgetField").style.background="url(includes/images/datepicker_open.png)"; alert(document.getElementById("widgetField").style.background);
However, I cannot access the URL property until it is set by JavaScript, even if it is already defined in the CSS file. Is there a reason the original CSS setting is not available?
javascript html css
user77413
source share