Last Updated: October 10, 2022
·
85.59K
· moiseevigor

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>"

Related protips:

fatal: refusing to merge unrelated histories

2 Responses
Add your response

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

over 1 year ago ·

thanks!

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside