-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\

2
  • What relevance do ios or objective-c or html tags have? Commented Jan 23, 2020 at 11:00
  • Welcome to C world. Commented Jan 23, 2020 at 11:31

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

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\\"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.