Friday Quick Tip: HTMLEditFormat for JavaScript

Posted By : todd sharp Posted At : July 24, 2009 10:43 PM Posted In: jQuery, JavaScript

4

I just came across a handy little function that emulates ColdFusion's HTMLEditFormat() function. It's quite simple really, but it worked perfectly since I wanted to display some HTML as text (instead of rendering it). Here it is should you ever finding yourself with a need to do the same:

function htmlEditFormat(h){
    return h.split("&").join("& amp;").split( "<").join("& lt;").split(">").join("& gt;");
}

Note: to use the function remove the space after each & - I had to put that in to prevent my blog from rendering them.

I should point also point out that Chris Jordan has a complete package of JavaScript functions that emulate ColdFusion built in functions on RIAForge called CFJS. The package is built to work with jQuery (FTW). I haven't tried it out yet, but from what I hear it comes in handy.

Comments (4)

Chris's Gravatar Todd,

I would also like to mention that I just within the last week or so added both the HTMLCodeFormat() and HTMLEditFormat() functions to the library.

I've been kinda neglectful of the project recently, but I did manage to get these added per a user request.

Chris's Gravatar Oh! And the function count is now at 72. So I encourage you and your readers to check it out sometime. :)

Chris's Gravatar Gosh, last post I promise!

CFJS is also available in a non-jquery flavor for folks who don't use jQuery. :)

todd sharp's Gravatar Awesome - thanks for the heads up.