Solved

Backslash escapee 140211

Posted on 2014-02-11
2
206 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now