Solved

Backslash escapee 140211

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

708 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

17 Experts available now in Live!

Get 1:1 Help Now