Responsive Ads Here

Tuesday, 21 March 2017

how to upload an image dynamically using javascript

HTML:

<div id='clock'></div>
<input type='file' id='getval' /><br/><br/>


CSS:

#clock{
    background-image:url('');
    background-size:cover;
    background-position: center;
    height: 250px; width: 250px;
    border: 1px solid #bbb;
}

JS:

document.getElementById('getval').addEventListener('change', readURL, true);
function readURL(){
    var file = document.getElementById("getval").files[0];
    var reader = new FileReader();
    reader.onloadend = function(){
        document.getElementById('clock').style.backgroundImage = "url(" + reader.result + ")";        
    }
    if(file){
        reader.readAsDataURL(file);
    }else{
    }
}

No comments:

Post a Comment

On scroll Sticky footer

HTML5: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Sticky Footer | Co...

Contact Form

Name

Email *

Message *