0

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.

9
  • 1
    What is the eventual text that is sent to the src attribute? what data representation is this {{ asset('admin/img/1.jpg') }}? Commented Aug 22, 2017 at 15:27
  • 2
    take note that javascript is in the client side, {{ asset('admin/img/1.jpg') }} must be processed first in the backend before it will be sent to the client/browser. So assigning {{ asset('admin/img/1.jpg') }} via javascript and expecting it to pull the image asset from the backend is impossible since browser will treat it as a string Commented Aug 22, 2017 at 15:29
  • Open devtools and read what error message says. Commented Aug 22, 2017 at 15:30
  • @kennasoft {{ asset('admin/img/1.jpg') }} seems to be some sort of javascript that needs to be pre-processed, which will have the value of the return of asset('admin/img/1.jpg') call Commented Aug 22, 2017 at 15:31
  • 2
    @CarlosJaramilloCorrales brother, you can't do that. As I said above, {{}} belongs to laravel or the backend which simply means, that it needs to be parsed or processed by laravel first. Backend is different from frontend. If your way is injecting {{}} via javascript where it will be parsed in the browser and laravel at that time is done parsing, hence, it won't going to work. Know the process brother. Commented Aug 22, 2017 at 15:40

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

it does the same thing my friend.
Only trying to help :)
0

Looks like your are using Angular or some similar templating engine like handelbars.js maybe Laravel.

This should work as expected if info[13] is a valid image url:

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

The call {{ asset('admin/img/1.jpg') }} is not yet a valid url. You first have to process this as mentioned in the comments above to get the absolute url path of the asset.

You can change an image on the client side like this:

https://jsfiddle.net/0e8u38oo/

Hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.