var input;
// method 1
input = document.getElementById('address').value;
alert(input)
// method 2
eval('input = "'+document.getElementById('address').value+'"')
alert(input)
method 1 is working fine, but method 2 is not working when newline characters are inputted and it says "unterminated string literal".
I need to store values using eval anyway. So please help me.
To use a special character as a regular one, prepend it with a backslash: \. . That's also called “escaping a character”.
String − We can pass any HTML string as an argument to escape special characters and encode it.
Special characters can serve different functions in the query syntax. To search for a special character that has a special function in the query syntax, you must escape the special character by adding a backslash before it, for example: To search for the string "where?", escape the question mark as follows: "where\?"
In the platform, the backslash character ( \ ) is used to escape values within strings. The character following the escaping character is treated as a string literal.
Use string.replace(/\r?\n/g, "\\n")
to escape the newlines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With