set attribute background image with javascript code example
Example 1: set an attribute background image javascript
document.body.style.backgroundImage = "url('img_tree.png')";
Example 2: set an attribute background image javascript
function bg() {
var imgCount = 3;
var dir = 'http://local.statamic.com/_themes/img/';
var randomCount = (Math.floor(Math.random() * imgCount));
var images = ['001.png', '002.png', '003.png'];
document.getElementById('banner').setAttribute("style", "background-image: url(" + dir + images[randomCount] + ");background-repeat: no-repeat;background-size: 388px 388px");
}
bg();