-2

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.

blex
  • 23,736
  • 5
  • 39
  • 70

2 Answers2

3
document.write('&lt;Place>');

You have to represent the special character <, using the HTML entity &lt;. Otherwise, your string will be interpreted as a HTML tag.

Paul Draper
  • 71,663
  • 43
  • 186
  • 262
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('&lt;Place&gt;');
Community
  • 1
  • 1
Daniel
  • 26
  • 3