Home > Back-end >  I want to change css background url by javascript
I want to change css background url by javascript

Time:10-13

I want to change the background url of image by javascript now in other tutorials they have the image downloaded in the desktop and just give its path but in my case the user will give a image link in input area and it should display inside of that url

let imglink = document.getElementById("img_link").value
document.getElementById("imgPreview").style.backgroundImage = 'url('   imglink   ')'

I know my code is wrong and weird but I need proper solution how i can put that link inside that url. PLEASE dont do too much hard coding bcz I am a beginner it will hard for me to understand

CodePudding user response:

Change

'url('   img_link   ')'

to

'url("'   img_link   '")'

CodePudding user response:

Does this work?

document.getElementById("imgPreview").style.backgroundImage = url(img_link)

CodePudding user response:

I think your code is correct; Just a slight improvement:

let imglink = document.getElementById("img_link").value
document.getElementById("imgPreview").style.backgroundImage = url(img_link)

Thank You! Hope that helps!

  • Related