Friday, March 20, 2009

JQuery & Html Entities

htmlentities() is well known for PHP developers. JQuery can do the same thing at client side. Here is the interesting blog : http://debuggable.com/posts/encode-html-entities-with-jquery:480f4dd6-13cc-4ce9-8071-4710cbdd56cb

Actually, it is quite simple: $('#content').text('This is fun & stuff').html(); // evaluates to "This is fun & a m p ; stuff"

However, i have another issue. I send a json data { myValue: 'AT& a m p ;T' } to jquery. And what jquery is supposed to do is $("#myInput").val(myValue). This, however, will display AT & a m p ;T in the input field, instead of AT&T, the one i really want to show.

Instead of writting your own html entities parser, here is a hacky solution:
var realValue =$("#js_temp").html(myValue).text();
$("#myInput").val(realValue);

No comments: