3

I have the following script:

function getMoods(nb) {
    var index;
    var a = ["Banana", "Coconut", "Peach", "Apple", ...];
    for (index=0; index<nb; ++index) {
        alert('a');
        if(index==1 || index==5 || index==9 || index==13) { moods += '<div class="col-xs-4">'; }
            moods += '
                <div class="checkbox">
                    <label for="'+a[index]+'">
                        <input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]+'
                    </label>
                </div>';
        if(index==4 || index==8 || index==12) { moods += '</div> '; }
    }
    $("#moods-area").html(moods);
}

I do not understand why I have the following error:

SyntaxError: Unexpected EOF

Could you please help me ?

2
  • 7
    Javascript doesn't support multiline strings
    – Oriol
    Oct 12, 2014 at 13:19
  • extension to @Oriol: use '+(NEWLINE)'... Oct 12, 2014 at 13:27

1 Answer 1

6

There are two problems:

  1. A wrong use of the spread operator ...:

    ["Banana", "Coconut", "Peach", "Apple", ...];
    

    This throws SyntaxError: expected expression, got ']', because after the spread operator there isn't any iterable object.

  2. JavaScript doesn't support multiline strings.

    You can use some alternatives:

    • Concatenate multiple strings

      moods += 
        '<div class="checkbox">'
          +'<label for="'+a[index]+'">'
            +'<input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]
          +'</label>'
        +'</div>';
      
    • Use \ at the end of each line to continue the string at the next one

      moods += '\
        <div class="checkbox">\
          <label for="'+a[index]+'">\
            <input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]+'\
          </label>\
        </div>';
      
    • Join an array of strings:

      moods += [
        '<div class="checkbox">',
          '<label for="'+a[index]+'">',
            '<input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index],
          '</label>',
        '</div>'].join('');
      
1
  • Thanks any idea why I have object HTMLInputElement in my page after ?
    – Geronimo
    Oct 12, 2014 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.