Getting SyntaxError: missing ] after element list

This is the error I'm getting:

SyntaxError: missing ] after element list

note: [ opened at line 959, column 12

This is refered output:

var specifications = [
   [ "<option value="'1'">"'CPU'"</option>" ],
   [ "<option value="'2'">"'ram'"</option>" ],
   [ "<option value="'3'">"'LCD'"</option>" ],
   [ "<option value="'4'">"'Mouse'"</option>" ],
   [ "<option value="'5'">"'Graphic'"</option>" ],
   [ "<option value="'6'">"'Keyboard'"</option>" ],
];

This is my code:

var specifications = [
    @foreach($specifications as $specification)
        [ "<option value="'{{ $specification->id }}'">"'{{ $specification->title }}'"</option>" ],
    @endforeach
];

How to fix this?

Answers:

Answer

You have unescaped quote marks in your strings.

Consider one of the inputs: "<option value="'1'">"'CPU'"</option>". It starts with " and ends with ". But it also has bunch of "s in the middle, which confuses the parser. So any "s you have in-between you have to escape by changing them to \":

var specifications = [
    @foreach($specifications as $specification)
        [ "<option value=\"'{{ $specification->id }}'\">\"'{{ $specification->title }}'\"</option>" ],
    @endforeach
];

Edit: actually, it looks like you don't need so many quote marks. Consider changing

"<option value="'1'">"'CPU'"</option>"

to

"<option value='1'>CPU</option>"

In that case you don't need to escape anything and your code will look a little cleaner:

var specifications = [
    @foreach($specifications as $specification)
        [ "<option value='{{ $specification->id }}'>{{ $specification->title }}</option>" ],
    @endforeach
];

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.