Home > Software design >  how to write html code and stop it from being executed ?? so that I can share a code as if its norma
how to write html code and stop it from being executed ?? so that I can share a code as if its norma

Time:11-21

it's simple html tag related problem, I guess.

I want to share few lines of code in my blog, and I want to write <h1> heading</h1>. visitors must see <h1> heading </h1>, and not just heading. do I need to use JavaScript for this, please help me with this problem.

CodePudding user response:

you can use HTML Entities like &lt , &gt

<h1>&lt;h1&gt;heading;/h1&gt;</h1>

and this link may help you https://www.w3schools.com/html/html_entities.asp

CodePudding user response:

There are two ways of doing this:

  1. Escape the Characters using HTML Entities or Using a JS Library
  2. Dynamically add the content using JavaScript textContent Property.

Here is a Sample code using both:

const codeSnippet = document.getElementById("codeSnippet");
codeSnippet.textContent = "<h1>Heading</h1>";
<div>
  <p>Escaping:</p>
  <code>
    &lt;h1&gt;Heading&lt;/h1&gt;
  </code>
</div>

<div>
  <p>Dynamically added usisng JS:</p>
  <code id="codeSnippet"></code>
</div>

  •  Tags:  
  • html
  • Related