Bogdan
Bogdan

Reputation: 713

javascript escape quotes and vars

I have the following piece of code that appends on different divs data from mysql but I have a problem with adding the image I can't escape the " correctly.

   $(document).on("click", ".active-modal", function () {
     var queryId = $(this).data('uri');
     $.ajax({
       url: '/catalog/product/modal-ajax',
       type: 'POST',
       dataType: 'json',
       async: true,
       data: {productId: queryId},
       success: function(data) {
          for (var key in data) {
            $('.product-name').text(data.ProductName);
            $('.product-code').text(data.BexProductCode);
            $('<img src="img/door-right.png">').appendTo(".ball_footballbox");
            $('<img src="/public/uploads/productPictures/thumbnails/" '+ data.ProductImage +' ">');
          }
       }
     })
     .fail(function() {
       console.log("error");
     })
   });

Upvotes: 0

Views: 59

Answers (2)

Hardy
Hardy

Reputation: 5631

Just change this:

$('<img src="/public/uploads/productPictures/thumbnails/" '+ data.ProductImage +' ">');

to this:

$('<img src="/public/uploads/productPictures/thumbnails/'+ data.ProductImage + '" />').appendTo('#some-destination');

or you can put the src like this:

var image_source = '/myimage/dir/' + data.ProductImage;

$('<img />').attr('src', image_source).appendTo('#some-holder');

Upvotes: 1

vnd
vnd

Reputation: 356

If I understood your problem correctly, you need to change this line:

$('<img src="/public/uploads/productPictures/thumbnails/" '+ data.ProductImage +' ">');

to this one:

$('<img src="/public/uploads/productPictures/thumbnails/'+data.ProductImage+'">');

If data.ProductImage contains some quotes try to replace any " occurrence by &quot; entity and then paste in into src attribute.

Upvotes: 2

Related Questions