<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?
innerHTML, which will interpret the<p>tags rather than displaying them. I think the browser is doing correctly what you've asked, but not what you expectJSON.stringifyanything that isn't an array or object. The result is just a JSON string (I'm not sure if that result is guaranteed; that's just what V8 seems to return), but not a valid JSON text, so it can be ignored by JSON parsers.[or{. That's not made very clear on json.org .