Reputation: 2848
var foo = $('.imgEscape').text();
foo = foo.replace("<img src="", "<img src='");
foo = foo.replace("" />", "' />");
$('.imgEscape').text(foo);//will display as string
$('.imgEscape').html(foo);//will display all html tag
<img src="https://www.google.com.tw/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" />
<b>abc</b>
I have a page backend output data as htmlspecialcharacter.
I wish to use js to replace some tag (ex. img tag)
so I can still display image
my problem is if I use html()
, it will display all element.
I wish to display image only (<img> ... <b>abc</b>
)
anyone know how to do this?
Upvotes: 1
Views: 45
Reputation: 115222
The text()
method returns the parsed text data so String#replace
method is doing nothing since the content is parsed string. Instead you need to get the actual HTML
content content using html()
method .
var foo = $('.imgEscape').html();
// update here -----------^^^^^^----
foo = foo.replace("<img src="", "<img src='");
foo = foo.replace("" />", "' />");
$('.imgEscape').html(foo);
Upvotes: 3