views:

205

answers:

3

Quick question, If I want to document some code on a basic HTML and put that within a CODE tag, how can I quickly convert the code between those tags when the page renders to display properly? I know I can write a javascript find and replace and search through the string over and over until its done replacing all the characters, but is there a better way?

Or, is there a jQuery way to do it if I need to use javascript?

+6  A: 

I think the <code> tag is more for displaying with a certain font, rather than layout. <code> seems to just use a monospaced font.

You might be looking for the <pre> tag (for pre formatted). That will preserve line breaks and spaces.

Unless the code you are trying to display is HTML code itself, then I think you'd have to change all the <'s to &lt;'s ahead of time

Grant
Ates Goral
Actually, ampersands display correctly by them selves as long as they don't end up matching and existing html entity code. You are right that they probably should be "escaped". Replacing the >'s is just a matter of preference, I personally prefer to see the > to remind me that there's a tag there.
Grant
+1  A: 

Sounds like you may be looking for syntax highlighting. Take a look at google's syntax highlihter

Mike Griffith
I'm loving the syntax highlighter, thanks!
FluidFoundation
A: 

Both good suggestions! Thanks!

FluidFoundation