1

How to get typed code in output in html?

I have code that I want to insert in the same way inside the html tag and display in the same way.

<body>
  <code>
    <script>alert("hey!");</script>
  </code>
</body>

I have the above and I expect my html output to be as follows. And this code is typed in the output, not executed!

<script>alert("hey!");</script>

2 Answers 2

1

&lt;script>alert("hey!");&lt;/script>

Just use &lt; for the < at the beginning of a tag.

<body>
  <code>
    &lt;script>alert("hey!");&lt;/script>
  </code>
</body>

Sign up to request clarification or add additional context in comments.

Comments

1

You can use special symbols like this

< -> &lt;/&#60;
> -> &gt;/&#62;
& -> &#38;
# -> &#35;
( -> &#40;
) -> &#41;
" -> &#39;/&#34;/&quot;
$ -> &#36;
/ -> &#47;
\ -> &#92;
] -> &#93;
[ -> &#91;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.