Search code examples
javascriptobject-expected

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


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


Solution

  • 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>';