1

I want to display html code in html,I tried solution from SO itself, but that was not helpful or may be I'm missing something. Then, I tried following syntax, but not helpful (only getting Click Here to Apply).

<pre><code><span style='font-size:20px'>Click Here to Apply </span></code></pre>

Anyone help me to figure out, what's wrong in my approach ?

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • Is your code being generated dynamically? – Vaughan Hilts Aug 04 '13 at 06:53
  • no, i'm writing that code in html – Ravi Aug 04 '13 at 06:54
  • possible duplicate of [Display HTML code in HTML](http://stackoverflow.com/questions/2820453/display-html-code-in-html) – user123444555621 Aug 04 '13 at 06:59
  • Please read ALL answers and comments in the linked question – user123444555621 Aug 04 '13 at 07:00
  • @Pumbaa80 but I think I followed all the answers and comment from that post. And, as OP of that post included same problem as I pointed here too that **replacing each `<` and `>` chars to `<` and `>` isn't seems to be efficient solution** Also, in my second approach shows, I even tried `
    ` too, but that also not helpful.
    – Ravi Aug 04 '13 at 07:04
  • The answers clearly state that you have to escape the contents of `
    `, but you don't need to escape the contents of `
    – user123444555621 Aug 04 '13 at 07:11

6 Answers6

3

You have to replace all '<' characters with &lt; and all '>' with &gt;

Note this won't provide code coloring though if that's what you seek.

Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38
Tomer W
  • 3,395
  • 2
  • 29
  • 44
3

Tomer's answer is correct and that is the way that I would do it. But there is an alternative that uses JavaScript. You can remove the HTML of an element, and add it back as text using createTextNode(). Pass an element to this function:

function revealElementHTML(el) {
    var html = el.innerHTML;
    el.innerHTML = "";
    el.appendChild(document.createTextNode(html));
}

jsfiddle.net/rh7RW

Community
  • 1
  • 1
gilly3
  • 87,962
  • 25
  • 144
  • 176
2

There used to be an xmp tag to support this - this tag is now deprecated but is still supported by all major browsers.

You can use it if nothing else suits your needs.

<pre><xmp><span style='font-size:20px'>Click Here to Apply </span></xmp></pre>
NoviceProgrammer
  • 3,347
  • 1
  • 22
  • 32
0

if you require colorized snippests, jQuery.snippest offers a nice colorize tool.

http://www.steamdev.com/snippet/

Tomer W
  • 3,395
  • 2
  • 29
  • 44
0

you should use the ascii characters to show your code or you have to use javascript as on gilly3's answer.

you can convert your html code online.

If you use php, use htmlentities.

finally you can use following css to style it.

pre {
 white-space: pre-wrap; /* css-3 */
 white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
 white-space: -pre-wrap; /* Opera 4-6 */
 white-space: -o-pre-wrap; /* Opera 7 */
 word-wrap: break-word; /* Internet Explorer 5.5+ */
}
Janith Chinthana
  • 3,792
  • 2
  • 27
  • 54
0

i have try all of them, the pre/code is not really solution, but we have other 3 simple solution

  1. textarea
<textarea name="tex" id="texa" cols="30" rows="10"class="one textarea">
        here is ok, mama? <a href="#">aoeueouoe</a> 

</textarea>
texa.value+='<br>\n\n'+i; 

  1. xmp
<xmp>
    keyi1xiedaimama? <a href="#">aoeueouoe</a>      
</xmp>
  1. createTextNode
info.appendChild(document.createTextNode(texa.value));

defend orca
  • 617
  • 7
  • 17