Encode HTML entities with jQuery

Posted by Felix Geisendörfer, on Jul 21, 2007 - in JavaScript & jQuery

Hey folks,

just a quick tip, if you ever need to use something like PHP's htmlentities() function in JS and happen to use the excellent jQuery library, here is how to do it:

javascript
  1. $('<div/>').text('This is fun & stuff').html(); // evaluates to "This is fun &amp; stuff"

Enjoy the magic,
-- Felix Geisendörfer aka the_undefined