2

My view like this :

<li id="thumbnail-view" class="count-photo">
    <div class="thumbnail">
        <img src="{{ asset('img/thumbs/'.$photo) }}" alt="">
        <a href="javascript:" class="thumbnail-check"><span class="fa fa-check-circle"></span></a>
        <ul class="list-inline list-edit">
            ...
        </ul>
    </div>
</li>

My javascript like this :

var photo = 'flower.jpg';
var res = `<img src="{{ asset('img/thumbs/'+photo) }}" alt="">`
$('#thumbnail-view img').html(res);

If the javascript code executed, I want to change the image

I try like that, but it does not work

How can I do it?

3
  • 2
    use .attr("src", "linkhere") Commented Jun 8, 2017 at 10:04
  • I don't think your question is clear enough!"{{ asset('img/thumbs/'.$photo) }}" is like a template, i think you must follow the template Convention Commented Jun 8, 2017 at 10:14
  • Just use attr. Its a jquery function. You can set or get any attribute of a selected element. Commented Jun 8, 2017 at 10:15

3 Answers 3

5

Simple way

 var photo = 'flower.jpg';
 $('#thumbnail-view img').attr('src', photo);
Sign up to request clarification or add additional context in comments.

Comments

3

Don't set the inner HTML of an image, set its src property.

$('#thumbnail-view img').prop("src", "{{ asset('img/thumbs/') }}"+photo);

Sidenote: if this is in a .js file then your template engine probably will not process the asset tag so you need to hardcode the path or grab it from elsewhere.

$('#thumbnail-view img').prop("src", "/assets/img/thumbs/"+photo);

1 Comment

I need you help. Look at this : stackoverflow.com/questions/44674975/…
1

You should use like this: $('#thumbnail-view img').attr('src','path/to/photo');

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.