-2

I'm trying to pass slash keyword "\" by using javascript. But not taking the slash in text field.

Example:

document.getElementById("demo1").value = "welcome\"
<!DOCTYPE html>
<html>
<body>
  <h2>JavaScript String Properties</h2>
  <p>The length property returns the length of a string:</p>
  Username : <input id="demo1" />
</body>
</html>

Output:

JavaScript String Properties

The length property returns the length of a string:

Username : [It's empty field]

How to pass the "welcome\".

Could please help me how I will get below output.

Username : welcome\

Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
Yalamandarao
  • 3,852
  • 3
  • 20
  • 27

3 Answers3

2

Just append one more \ to escape the string

document.getElementById("demo1").value = "welcome\\";
<!DOCTYPE html>
<html>
<body>
  <h2>JavaScript String Properties</h2>
  <p>The length property returns the length of a string:</p>
  Username : <input id="demo1" />
</body>

</html>
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
1

\ (Escape character) is a reserved character and it has a special meaning. You have to escape with another \ to evaluate that literally:

<h2>JavaScript String Properties</h2>
<p>The length property returns the length of a string:</p>
Username : <input id="demo1"/>

<script>
  document.getElementById("demo1").value = "welcome\\"
</script>
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

You have to use an escape character(prepend it with a backslash) to get the expected result.

More precisely, it should be

"welcome\\"



document.getElementById("demo1").value = "welcome\\"
Arun Ramachandran
  • 1,270
  • 5
  • 23
  • 36