xxxxxxxxxx
const bgImage = document.querySelector('.classname')
bgImage.style.backgroundImage = "url('your-image.png')"
// Javascript createElement
const divBackground = document.createElement("div")
divBackground.style.background = 'url('your-image.png')'
document.body.appendChild(divBackground)
xxxxxxxxxx
var element = document.getElementsByClassName('class-name');
element.style.backgroundImage = 'url("../image.png")';
xxxxxxxxxx
//Dynamically set the background image using js
let imageLink = 'https://images.pexels.com/photos/3928264/pexels-photo-3928264.jpeg?auto=compress&cs=tinysrgb&w=600&lazy=load'
document.getElementById(
'body'
).style.backgroundImage = `url(${imageLink})`;
//Note that the url is encompassed by backticks, not single quotations.
//Backticks can be gotten on keyboard
//You can build a function and make imageLink a parameter or do anything else.
//There are many thinngs