JavaScript: BackSlash as part of a string

I have a JavaScript variable that I'm using PHP that is shown in the page source:

var db_1 = 'C:\this\path'; 

When I set the value of the text field with this variable as follows:

 $('#myinput').val(db_1); 

The slashes disappeared, and only the remaining characters remained!

Why is this and how can I return slashes to

Thanks everyone

+7
javascript jquery
source share
3 answers

The backslash is an escape character in JS. They get lost when the string literal is parsed.

You cannot return them because you cannot tell where they are. You need to make sure that they remain in the string first (by representing them using an escape sequence).

 var db_1 = 'C:\\this\\path'; 
+11
source share

You can use:

 echo json_encode('C:\this\path'); 

json_encode can be used as a filter function for some JavaScript code.

+2
source share

Try the following:

 var db_1 = 'C:\\this\\path'; 

For more information: http://www.w3schools.com/js/js_special_characters.asp

+1
source share

All Articles