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
Solved

Backslash escapee 140211

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

856 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