views:

31

answers:

2

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!

A: 

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/

Dr.Molle
A: 

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);
Nick Craver