javascript - Change Background Image in a loop -


to me, seems simple idea- when click div, background image of div changes. far, i've tried colors (put color names in array, change "box.style.backgroundimage" "box.style.backgroundcolor") works fine colors, not images. idea why?

javascript:

var box = document.getelementbyid('box'), imgs = ['/image.jpg', '/image2.jpg'];  box.onclick = function () { img = imgs.shift(); imgs.push(img);  box.style.backgroundimage = img; }; 

html:

<div id='box'></div> 

css:

#box { background: url("https://priteshgupta.com/wp-content/uploads/2011/06/html-ipsum.png"); width:200px; height:200px; margin:50px; } 

you still need url("..."):

box.style.backgroundimage = 'url("' + img + '")'; 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -