Wednesday, September 21, 2011

Encode HTML Characters in JavaScript using jQuery

Here's a quick post on how to encode and decode HTML characters in JavaScript using jQuery. I didn't come up with the solution, but I thought it was pretty clever.
function htmlEncode(value){
    return $('<div/>').text(value).html();
}

function htmlDecode(value){
    return $('<div/>').html(value).text();
}
Courtesy of http://stackoverflow.com/questions/1219860/javascript-jquery-html-encoding

2 comments:

  1. Thanksgiving day is around the corner. How are you going to celebrate it? You must have noticed that all places are holding promotional activities to promote sales of their products. No matter the supermarket, store or the mall, you can always get products at much cheaper prices. It is also a good choice to come to 4wowgold.com to get cheap wow gold now. Seize your time.

    ReplyDelete
  2. I was trying to use this method to encode ENdash and EMdash but this methos does not work out. can you tell how to encode them.

    ReplyDelete

Note: Only a member of this blog may post a comment.