My question is how to change the background-image of a div to an image selected by an input file element. The file-input is hidden and executed when the user of the site clicks on another input element here is that code:
$(function(){
$('#verborgen_file').hide();
$('#uploadButton').on('click',function(){
$('#verborgen_file').trigger('click');
});
});
So the file-input is #verborgen_file and the other input is #uploadButton now I'm looking for a way that when the user clicks on #uploadButton and therefore triggers #verborgen_file and choses an image (and only an image) the background-image of the div #pf_foto changes to that chosen image.
I have try'd a lot of things but just couldn't get it to work so in advance Thank You!
----EDIT----
i try'd this:
$("#verborgen_file").on("change", function(){
var files = !!this.files ? this.files : [];
if ( !files.length || !window.FileReader ) return;
if ( /^image/.test( files[0].type ) ) {
var reader = new FileReader();
reader.readAsDataURL( files[0] );
reader.onloadend = function(){
$("#pf_foto").css("background-image", "url(" + this.result + ")");
}
}
});