Hunali
Hunali

Reputation: 277

How to change background image from user input (URL) using JavaScript

How I'm supposed to change the HTML background image using the input user, the input is URL Link.

    <input type="text" id="value">
    <button onclick="fun()">Change</button>

I tried something like this but it does not work.

function fun (){
      var value = document.getElementById("value").value;
      document.body.style.background = 'url(value)';
    }

Upvotes: 0

Views: 348

Answers (1)

Swetank Poddar
Swetank Poddar

Reputation: 1291

Its because you are not using the value of "value". You need to concat the variable between the string.

Try this:

<input type="text" id="value">
<button onclick="fun()">Change</button>
function fun(){
      var value = document.getElementById("value").value;
      document.body.style.background = "url("+ value + ")";;
}

Upvotes: 1

Related Questions