Preview input field does not work

Asked

Viewed 20 times

1

my browser is not displaying the image. Fear something wrong with this code?

$(document).ready(function ()
{
     $("input[name='fun_imagem']").change(function ()
     {
         readImage($(this));
     });

});


function readImage(input) {

     var arquivo = new FileReader();
     arquivo.onload = function(e)
     {
          $("#imagem-figure-usuario").attr("src", e.target.result);
     };

     arquivo.readAsDataURL(input.files[0]);
}

1 answer

1


I noticed that you are passing a list of jQuery elements to the function readImage.

readImage($(this));

You just need to stop covering this with the $ jQuery. If you haven’t noticed, this is a unique HTML element, the element in the list $("input[name='fun_imagem']") that was clicked. jQuery prototype does not offer a property files, nor function. Then:

readImage(this);

If it was already on a jQuery list (in other cases) you would only need to index with a specific index, for example: $ls[0], would return the first element of the list.

  • Dude, you’re absolutely right. It’s working perfectly now. You have some explanatory link about this difference between $(this) and this?

  • @Pedrosoares I found one for you at Sopt: http://answall.com/questions/37770/qual-a-diferença-entrethis-e-this-e-this

Browser other questions tagged

You are not signed in. Login or sign up in order to post.