I have the following HTML:
<div id="tab1" style="position:relative; background-image:url(buttons/off.png);
<a href="javascript:ChangeBackgroundImageOfTab('tab1', 'on');">
<img id="DivBtn1" name="DivBtn1" src="buttons/text.png" >
</a>
</div>
and the following Javascript:
function ChangeBackgroungImageOfTab(tabName, imagePrefix)
{
document.getElementById(tabName).style.background-image= 'url("buttons/" + imagePrefix + ".png")';
}
The issue arises when i try to set the tabs background image via a call to getElementByID - I do now know how to create a dynamic URL that uses the parameter that was passed in, along with some other hard coded values. In this case, we are swapping the OFF background image with the ON background image.
How can i do this? Is there some way to use a javascript variable, assign the full path to it, then send it into the call as the background image path?
This question is related to
javascript
html
css
url
From what I know, the correct syntax is:
function ChangeBackgroungImageOfTab(tabName, imagePrefix)
{
document.getElementById(tabName).style.backgroundImage = "url('buttons/" + imagePrefix + ".png')";
}
So basically, getElementById(tabName).backgroundImage
and split the string like:
"cssInHere('and" + javascriptOutHere + "/cssAgain')";
If you are looking for a direct approach and using a local
File in that case.
Try
<div
style={{ background-image: 'url(' + Image + ')', background-size: 'auto' }}
/>
This is the case of JS
with inline styling where Image
is a local file that you must have imported with a path.
Source: Stackoverflow.com