0

add src of image using jquery append function. i am using a variable in foreach loop and i wants to append image src . how do i do that.

my jquery code

$.each(data.products, function(key,val) {
  $("div#yourwishlist").append('<div class="col-sm-6 col-md-4"><article class="box"><figure><a class="hover-effect" title="" href="#"><img width="300" height="160" alt="" src="'+val('thumb')+'"></a></figure><div class="details"><a class="pull-right button uppercase" href="" title="View all">select</a><h4 class="box-title">'+val['name']+'</h4><label class="price-wrapper"><span class="price-per-unit">$170</span>avg/night</label></div></article></div>');
console.log(val['name']);
});

this code is not working in my case ...

3
  • squere brackets here val('thumb'), something like val['thumb'] Commented Aug 4, 2016 at 12:08
  • change val('thumb') to val['thumb'] Commented Aug 4, 2016 at 12:09
  • when i am alerting val['thumb'] . it shows right values ... but i think i am writing some syntax error in src="'+val('thumb')+'" . as i am view sourcing its not showing the value like src="placehold.it/300x160" Commented Aug 4, 2016 at 12:13

1 Answer 1

1

Hitesh you can add src attribute to an image like:

$.each(data.products, function(key,val) {
   $("div#yourwishlist").append('<img width="300" height="160" alt="" src="'+ val +'">');
});

// here val contains the path of image in it
Sign up to request clarification or add additional context in comments.

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.