Search code examples
javascripthtmlprompt

How to turn HTML-format user input into text in prompt


I want to have a prompt box asking the user for their name. But I'm putting that directly into the page, and if I enter something HTML-y (<canvas> for example) then it places that element on the page. How can I make this into text, so instead of making a <canvas> element it writes <canvas> to the page as text?

var name = prompt("Enter your name: ");
document.getElementById("paragraph").innerHTML = name;
<p id="paragraph"></p>

Unfortunately I can't use jQuery, so a pure JS solution would be nice.


Solution

  • You can use innerText, instead of innerHTML

     document.getElementById("paragraph").innerText = name;