Search code examples
javascripthtmllaravel-5.3

Changing img src via JavaScript


I got this HTML image item:

<img class="profile-photo img-responsive img-circle" id="imagen_perfil_banner" src="{{ asset('admin/img/profile1.jpg') }}" >

Then I got the JS that changes the src attribute of the image like this:

document.getElementById("imagen_perfil_banner").src = info[13];

The info[13] variable has the next information:

{{ asset('admin/img/1.jpg') }}

When tha page loads, it just doesn't show the image that I send via JS, but when I put the image directly on the image src attribute, it loads just fine.


Solution

  • Please try this:

    var image = document.getElementById("imagen_perfil_banner")
    image.src=info[13];
    

    You firstly have to save the image in a variable. I hope it helps. Good luck.