user5970552
user5970552

Reputation: 303

"UnExpected Token Illegal" Error Message when prepend to DOM element using Jquery

I am trying to prepend using jquery and i am getting Unexpected Token Illegal error message. Kindly help. Below is my code:

<html>

  <body>
    <div class="container-fluid">
      <header>
      </header>
    </div>

  </body>
</html>

My Javascript

var hamburger = "<button id='
hamburger1'><span class='
glyphicon - menu - hamburger '></span></button>";

var myapp = "<span class='app1'>My App</span><span class='app2'>|</span>";
 var myTopNav = hamburger + myapp;

jQuery('header').prepend(myTopNav);

Upvotes: 0

Views: 70

Answers (1)

Derek Pollard
Derek Pollard

Reputation: 7165

The problem with your code is that you are breaking the hamburger variable without adding the proper character to let the processor know you are continuing on the next line, which brings me to my next point:

There are 2 ways to break a Javascript string into new lines,

Backslashes:

var text = "Lorem ipsum dolor sit amet,\
consectetur adipisicing elit,\
sed do eiusmod tempor incididunt\
ut labore et dolore magna aliqua.";

And the ever so popular, plus sign (concatenation):

var text = "Lorem ipsum dolor sit amet,"
    + "consectetur adipisicing elit,"
    + "sed do eiusmod tempor incididunt"
    + "ut labore et dolore magna aliqua.";

Upvotes: 1

Related Questions