-1

In my profile I insert a button to load a picture from pc, but I don't know how upgrade the default user's image with the new image selected by user. Then I would like change the text of button from carica to elima when in the src there is an image loaded by user.

function changeImage(){

    var elem = document.getElementById("change_image_button");

    //Se devo caricare l'immagine
    if(elem.value=="Carica"){
        var input = document.createElement('input');
        input.type = 'file';
        input.onchange = e => { 
            var file = e.target.files[0]; 
            var reader = new FileReader();
            reader.readAsDataURL(file,'UTF-8');
            reader.onload = readerEvent => {
                var content = readerEvent.target.result; //File da caricare
                document.getElementById("profilePicture").src= content.src;
            }
        }
        input.click();
        elem.value = "Elimina";
    }

while the html code is:

<!--<Profile picture>-->
                        <img id="profilePicture" src="images/profilo.png" style="height: 173px;width: 160px;margin-left: 20%;margin-top: 20px;">
        <div id="infoLoginUtente">
            <p><span>Cambia Immagine: </span>
            <span> 
               <button onClick="changeImage();" id="change_image_button" value="Carica"> Carica</button> 
               <input id="file-input" type="file" name="name" style="display: none;" />
            </span></p>
        </div>    

Cambia Immagine: Carica

but when i try to put the image the result is: enter image description here

Alessia
  • 59
  • 7

1 Answers1

0

Modify your input.onchange like this:

input.onchange = e => { 
  var file = e.target.files[0];             
  document.getElementById("profilePicture").src= URL.createObjectURL(file);
}
gulima
  • 139
  • 2
  • 11