I am trying to print this text “<Place>”
in document.write in javascript.But I am not able to print it.Because of the “<“ sign it is not printing.Need help.
Asked
Active
Viewed 422 times
-2

blex
- 24,941
- 5
- 39
- 72

user5355171
- 35
- 6
-
I am trying to print “
" – user5355171 Apr 05 '16 at 21:28 -
[duplicate?](http://stackoverflow.com/questions/42182/how-to-escape-and-inside-pre-tags) – mejdev Apr 05 '16 at 21:31
-
I used “
”. But it is not working – user5355171 Apr 05 '16 at 21:32 -
Also, you should probably avoid `document.write`. It depends on your JS executing synchronously. – mpen Apr 05 '16 at 23:44
2 Answers
3
document.write('<Place>');
You have to represent the special character <
, using the HTML entity <
. Otherwise, your string will be interpreted as a HTML tag.

Paul Draper
- 78,542
- 46
- 206
- 285
-
This is the perfect answer. The last ">" does actually display since it has not opening (no "<"), so the navigator will interpret it as a normal character and thus, display it without the need to replace it with its code. – Ahmed Aboumalek Apr 05 '16 at 21:39
0
I agree with @Paul Draper's answer as being a solution for this question.
However, you should be aware of characters that have special meaning in HTML and always convert special characters to character entities.
If you are not careful and simply include the characters, than the browser will try to interpret them. This could lead to potential issues and big headaches where unwanted HTML elements might get included down the line.
In this particular example it would be wise to encode both <
and >
document.write('<Place>');