Skip to content
Advertisement

Set uploaded photo as website background image [closed]

I wrote a very simple image upload script, after uploading the image I thought of making the background of the linking screen the uploaded image but i have no idea how to do it or i dont know if it can be done. Can anyone help me?enter image description here

Advertisement

Answer

You can use following code

function change_img(){
var img_url = document.getElementById("url").value;
document.getElementsByTagName("body")[0].style.backgroundImage = "url('"+img_url+"')";
}
    <input type="url" name="" id="url" placeholder="Enter img url here">
    <button onclick="change_img()">change background</button>

First make a variable img_url and store url of image in it
Then use getElementsByTagName("body")[0] this selects the body tag and then use backgroungImage to set background image.
Remember in css we use background-image:url("image url") to set bacground using js So use " url(' " + img_url + " ') "

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement