Reputation: 305
I have a list set up in HTML as follows:
HTML:
<ul id="move_2">
<li id="mv_txt_2" >Move Section Up</li>
</ul>
<button onClick="position();">change the bg_image</button>
CSS:
#move_2 {
height:20px;
background:url(../images/mvup.fw.png) no-repeat;
padding-left:30px;
margin-top:15px;
vertical-align:middle;
list-style:none;}
#mv_txt_2{
padding-left:5px;
cursor:pointer;
font-size:14px;
vertical-align:middle;}
But the following javascript does not change the background image:
function position(){
document.getElementById('move_2').style.background='url(../images/mvdown.fw.png)';
}
I'd be grateful to receive any comments to solve this problem.
Upvotes: 0
Views: 88
Reputation: 5199
I think ../images works in CSS because your CSS is in a directory. When working with html you must use absolute path. Did you try :
document.getElementById('move_2').style.background='url(images/mvdown.fw.png)';
And use "onclick" instead of onClick.
Upvotes: 1
Reputation: 190
html:
onclick="..."
jquery:
$(document).ready(function(){
$("#move_2").css("background" , "yourAddress");
});
Upvotes: 0
Reputation: 22992
Try this:
<ul id="move_2">
<li id="mv_txt_2">Move Section Up</li>
</ul>
<button>change the bg_image</button>
#move_2 {
height:200px;
background:url(http://istheapplestoredown.com/images/dashboardwidget.png) no-repeat;
padding-left:30px;
margin-top:15px;
vertical-align:middle;
list-style:none;
}
#mv_txt_2 {
padding-left:5px;
cursor:pointer;
font-size:14px;
vertical-align:middle;
}
$('li').click(function position() {
document.getElementById('move_2').style.background = 'url(http://istheapplestoredown.com/images/websitewidget.png) no-repeat';
});
Upvotes: 2