1

How on earth do you encode HTML with jQuery/JavaScript?

Havn't found any working solution after hours of Googling and surfing this site.

This:

jQuery('<div/>').text(value).html();

does not work with jQuery!

2 Answers 2

2

What do you mean with "encode"?

If you mean something similar to PHP's htmlentities, your solution should work with jQuery:

http://jsfiddle.net/wrH8b/

Sign up to request clarification or add additional context in comments.

Comments

0

What you have works, you can test it here, make sure you're using that to get the encoded text.

If your goal is to encode it for transmission (e.g. a POST), you should be looking at encodeURIComponent() instead, for example:

var encodedString = encodeURIComponent(value);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.