Error: "Expected an identifier and instead saw '<'"

4.7k Views Asked by At

I am writing this in JS and trying to figure out what my editor is complaining about. Here is what I have:

document.write('<div id="email-widget"></div>');
  var widgetHTML = 
   <div id="email-widget-box">
    <div id="email-signup-form">
     <p><strong>Send deals directly to my inbox - Sign up now!</strong></p>

What is missing because my editor has 4 errors all for one line but I cannot figure this out. The errors are:

Expected an identifier and instead saw '<'
Missing semicolon
Expected an assignment or function call and instead saw an expression
Missing semicolon

1

There are 1 best solutions below

1
On BEST ANSWER

You start off just fine:

var widgetHTML = 

what follows that = should be a Javascript expression. Strings ("..."), numbers, variable names, function calls, any of those things combined with operators -- all would be fine.

But a bunch of HTML is not a Javascript expression.

<div id="email-widget-box">
 <div id="email-signup-form">
  <p><strong>Send deals directly to my inbox - Sign up now!</strong></p>

If you want to set widgetHTML to that HTML text, you'll need to make a string out of it:

var widgetHTML = '<div id="email-widget-box">' +
  '<div id="email-signup-form">' +
  '<p><strong>Send deals directly to my inbox - Sign up now!</strong></p>';