0

I am trying to create image tag string in javascript. This image tag in part of table row string. At the end I will append this table row string to mvc webgrid using jquery.

I have a javascript function which returns this new row

function getNewRow(email, friendlyName) {
 var imgSrc = "/Content/Images/User.png";

 // Create the new row html
 var newRow = '<tr class=\"' + rowClass + '\">' +    
'<td><text><img src\"' + imgSrc + '\"></text></td>' +
 '<td>' + email + '</td>' +    
 '<td>' + friendlyName + '</td>' +     
 '</tr>'
 return newRow;
  }

I will append returned string to webgrid object.

// Append the new Row
 $('#group').append(newRow);

But the image tag is not showing proper image. The forward slash is removed after appending. Used encodeURIComponent function but no use. How to make it to show the image properly.

PSR
  • 875
  • 2
  • 13
  • 37
  • 3
    `''` - need to have `=` after `src` and no need to escape `"` using `\"` – Arun P Johny Oct 26 '15 at 12:41
  • Possible duplicate of [How to create a new img tag with JQuery, with the src and id from a JavaScript object?](http://stackoverflow.com/questions/8013792/how-to-create-a-new-img-tag-with-jquery-with-the-src-and-id-from-a-javascript-o) – Lucas Rodrigues Oct 26 '15 at 12:41
  • You are missing a `=`after the src-attribute – Wa Kai Oct 26 '15 at 12:42
  • Possible duplicate of [How to escape a single quote ( ' ) in JavaScript?](http://stackoverflow.com/questions/16134910/how-to-escape-a-single-quote-in-javascript) – h2ooooooo Oct 26 '15 at 12:42
  • Sorry..My mistake. yes I forgot = symbol. Thanks. – PSR Oct 26 '15 at 12:46

2 Answers2

1

You have <img src\"' + imgSrc + '\"></text></td> when it should be <img src=\"' + imgSrc + '\"></text></td>. Notice the missing =.

Griffith
  • 3,229
  • 1
  • 17
  • 30
1

It seems that you have a typo in the second line of newRow declaration, you are missing the = sign after src attribute

Oday Fraiwan
  • 1,147
  • 1
  • 9
  • 21