Solved

Backslash escapee 140211

Posted on 2014-02-11
2
210 Views
Last Modified: 2014-02-12
Hello expert,

Working on using innHTML to change add some text to a page.

Ran onto the code copied below.  In the code
alert("The content of the first DIV element is " + "\"" + oVDiv.innerHTML + "\".");
it is understood that the backslash cancels out the next character.
So instead of  "\"" what is passed to the browser is "". But why use the backslash?
What would be wrong with just using ""?

Thanks.

Allen in Dallas

<!DOCTYPE html>
<html>

<head>
  <title>Show First DIV Content</title>
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  <meta http-equiv="X-UA-Compatible" content="IE=8">
</head>

<body>
  <div id="oDiv1">Div #1</div>
  <div id="oDiv2">Div #2</div>
  <div id="oDiv3">Div #3</div>
  <input type="button" value="Show First DIV Content" onclick="fnGetId()"> 
  <script type="text/javascript">
    function fnGetId() {
      // Display the content of the first DIV element in the collection.
      var oVDiv=document.getElementById("oDiv1");
      alert("The content of the first DIV element is " + "\"" + oVDiv.innerHTML + "\".");
    }
  </script>
</body>

</html>

Open in new window

0
Comment
Question by:9apit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39850331
it don't cancel it, it allow you to use it

you can't have : """
because " start AND end a string

So if you want to use " inside a string set with double quotes you need to escape the char "

OR use simple quote around: 'my string'
alert('The content of the first DIV element is "' + oVDiv.innerHTML + '".');

Open in new window

0
 
LVL 2

Accepted Solution

by:
DualCool earned 500 total points
ID: 39851032
** the short answer is your code will fail if you just use "" and when you click your button nothing will happen

you must have the proper syntax to achieve the desired result.

the backslashes are used to "escape" the quote characters inside of the string and to show where the first and last part of the string ends and the dynamic text starts/ends. the plus signs allow the dynamic text inside of the div to be used in the alerted string. this is called string concatenation.

Experiment with the different ways and alert the result to see the differences

hope this helps
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question