Set value with backslash to input in JQuery [duplicate] - javascript

This question already has answers here:
Escaping backslash in string - javascript
(5 answers)
JavaScript backslash (\) in variables is causing an error
(5 answers)
Closed 4 years ago.
I have some input
<input id = 'some_id' type='text'/>
I want to set value with backslash, for example:
$('#some_id').val('someval \')
and I'm getting error:
Uncaught SyntaxError: Invalid or unexpected token
How can I show string value with backslash in input ?

Escape the \ character like this: $('#some_id').val('someval \\')
$('#some_id').val('someval \\')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id = 'some_id' type='text'/>

Related

Why JSON.parse for one backslash returns two backslashes, but not four ones [duplicate]

This question already has answers here:
JavaScript: A BackSlash as part of the string
(3 answers)
How to deal with backslashes in json strings php
(6 answers)
Closed 3 years ago.
I have the string with backslashes:
const str = 'E:\\music\\post'; // It represents "E:\music\post" text in HTML
JSON.stringify(str) returns "the same" string '"E:\\music\\post"'. Why not '"E:\\\\music\\\\post"'?
JSON.parse('"E:\\music\\post"') will throws the error: Uncaught SyntaxError: Unexpected token m in JSON at position 4.
JSON.parse('"E:\\\\music\\\\post"') works OK.
But another strange thing is that JSON.parse(JSON.stringify("E:\\music\\post")) works well. Why?
That is the better workaround for it? I want to save the string to json file, and later to create an object from the json file.

How to add an input tag with JS function in PHP [duplicate]

This question already has answers here:
Escaping quotation marks in PHP
(7 answers)
Closed 6 years ago.
I have the following code:
echo ('
<input onclick='responsiveVoice.speak("Hello World");' type='button' value='🔊 Play'>
');
When the code is executed, I get the following error:
Parse error: syntax error, unexpected 'responsiveVoice' (T_STRING) in C:\xampp\htdocs\adiai\ai_main.php on line 70
I noticed that my IDE's colors are also getting 'mixed up'. I have a feeling that it is because of the ';' and several quotation marks within my echo statement.
How am I supposed to fix this?
Thanks!
You have single quotes inside and outside your string which caused javascript to misunderstand. Use double quotes outside instead.
Using the \ symbol could help too
echo ("
<input onclick='responsiveVoice.speak(\"Hello World\")' type='button' value='🔊 Play'>
");

JavaScript runtime error: Invalid character using JSON.parse [duplicate]

This question already has answers here:
jQuery.parseJSON single quote vs double quote
(4 answers)
Closed 7 years ago.
The following throws "JavaScript runtime error: Invalid character":
var opts = "[{'UOM':'E','Description':'E - Each'},{'UOM':'C','Description':'C - Per 100'},{'UOM':'M','Description':'M - Per 1000'}]"
var options = JSON.parse(opts);
What invalid character is in my JSON string? Thanks
You need to replace the apostrophes with escaped quotation marks (\").

JSON.parse not working for valid json [duplicate]

This question already has answers here:
json parse error with double quotes
(9 answers)
Closed 8 years ago.
I am using the below line in Javascript to parse a json string.
var obj = JSON.parse('{"respDataMap":{"userMessages":{"lbSearchHint":"Enteravalueandpress\"Enter\"orclickon\"Search\"#"}},"respErrorCode":"","respErrorMessage":""}');
The escaped double-quote character in the string is causing the json parsing to fail.
But, the same string pasted in online JSON validators is certified as valid.
How do I fix this?
Use double slash like for escape character
var obj = JSON.parse('{"respDataMap":{"userMessages":{"lbSearchHint":"Enteravalueandpress\\"Enter\\"orclickon\\"Search\\"#"}},"respErrorCode":"","respErrorMessage":""}');

Parsing JSON containing new line characters [duplicate]

This question already has answers here:
How do I handle newlines in JSON?
(10 answers)
Closed 8 years ago.
In my website I try to convert a string to JSON which contains a newline.
JSON.parse('{"hallo":"line1\r\nline2","a":[5.5,5.6,5.7]}');
This produces an "Unexpected token" error. Do I need to escape that somehow?
Yes, you should escape both \n and \r as they belong to the list of control characters. Full list of characters that need to be escaped can be found here. Your code would be
obj = JSON.parse('{"hallo":"line1\\r\\nline2","a":[5.5,5.6,5.7]}');
JSFiddle: link
Try:
JSON.parse('{"hallo":"line1\\r\\nline2","a":[5.5,5.6,5.7]}');

Categories