jt7ysq
Last Updated: February 25, 2016
·
29.22K
· moiseevigor
2e1e340e378a26f2c69c6dd418060374

Encode string to HTML entities via jQuery

Encode to HTML entities

jQuery('<div />').text('Some text with <div>html</div>').html()

produces

"Some text with &lt;div&gt;html&lt;/div&gt;"

To decode we just switch the methods

jQuery('<div />').html('Some text with &lt;div&gt;html&lt;/div&gt;').text()

produces

"Some text with <div>html</div>"
Say Thanks
Respond

1 Response
Add your response

11557
45c5254200dbfb316f6f6d4b69e570ef

you may also want to check alternatives and their comparison here: http://jsperf.com/htmlencoderegex/25

over 1 year ago ·