Reputation: 31840
<html>
<script>
function printStringLiteral(theInput) {
document.body.innerHTML += JSON.stringify(theInput.value)
}
</script>
<body>
<p>Text to modify:<p>
<input type="text" id = "textInput">
<a onclick="printStringLiteral(document.getElementById('textInput'));" href="javascript:void(0);">Get string literal</a>
</body>
</html>
I'm trying to create a simple web page that accepts text as input and returns a string literal, but this web page doesn't display the string literal correctly after it has been generated. I entered <p>"Hi!"</p>
as input, but "\"Hi!\"
was displayed as output, instead of the correctly formatted string literal. What will I need to do here in order to get the string to display properly?
Upvotes: 1
Views: 119
Reputation: 276446
Instead of setting the innerHTML, set the text
function printStringLiteral(theInput) {
document.body.textContent += JSON.stringify(theInput.value)
}
If you need to support IE8- you need to set innerText
too
Upvotes: 3