Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

315
Views
Why does my code throw an invalid or unexpected token error?

Why does my code not work? it seems that the interpreter does not recognize the listItem variable as a string of new elements. It does not seem to work for some reason.

const draggable_list = document.getElementById("draggable-list")
const check = document.getElementById("check")

const richestPeople = [
    'Jeff Bezos',
    'Elon Musk',
    'Bernard arnault',
    'Bill Gates',
    'Mark Zuckerberg',
    'Warren Buffet',
    'Larry Ellison',
    'Larry Page',
    'Sergery Brin',

]

const listItems = []
let dragStartIndex
//createList()

function creatList(){
    [...richestPeople.forEach((person, index) => {
        const listItem = document.createElement('li')
        listItem.setAttribute('data-index', index)
//the code below does not seem to light up as a string. 
        listItem.innerHTML = '
            <span class="number">${index + 1}</span>
            <div class="draggable" draggable="true"></div>
            <p class="person-name">${person}</p>
            <i class="fas fa-grip-lines"></i>
        '
    listItems.push(listItem)
        draggable_list.appendChild(listItem)

    })]

    listItems.push(listItem)
    draggable_list.appendChild(listItem)

}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use backticks instead of single quotes:

const draggable_list = document.getElementById("draggable-list")
const check = document.getElementById("check")

const richestPeople = [
    'Jeff Bezos',
    'Elon Musk',
    'Bernard arnault',
    'Bill Gates',
    'Mark Zuckerberg',
    'Warren Buffet',
    'Larry Ellison',
    'Larry Page',
    'Sergery Brin',

]

const listItems = []
let dragStartIndex
//createList()

function creatList(){
    [...richestPeople.forEach((person, index) => {
        const listItem = document.createElement('li')
        listItem.setAttribute('data-index', index)
//the code below does not seem to light up as a string. 
        listItem.innerHTML = `
            <span class="number">${index + 1}</span>
            <div class="draggable" draggable="true"></div>
            <p class="person-name">${person}</p>
            <i class="fas fa-grip-lines"></i>
        `
    listItems.push(listItem)
        draggable_list.appendChild(listItem)

    })]

    listItems.push(listItem)
    draggable_list.appendChild(listItem)

}

about 4 years ago · Juan Pablo Isaza Report

0

There were a few issues that I ran into debugging your code.

  1. The first thing I noticed was you're function is named creatList, which appears to be a typo that is likely to get you into trouble. While you can obviously name the function anything you want, I went ahead and renamed it to createList in order to avoid confusion.

  2. You need to use backticks ` rather than single quotes to enclose multi-line strings. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

  3. I also cleaned up your forEach implementation in the createList function, then added an EventListener to call the function when the DOM is ready.

The code below works:

const draggable_list = document.getElementById("draggable-list");
const check = document.getElementById("check");

const richestPeople = [
    'Jeff Bezos',
    'Elon Musk',
    'Bernard arnault',
    'Bill Gates',
    'Mark Zuckerberg',
    'Warren Buffet',
    'Larry Ellison',
    'Larry Page',
    'Sergery Brin',
];

const listItems = [];
let dragStartIndex;

//createList()

function createList() {
    richestPeople.forEach((person, index) => {
        let listItem = document.createElement('li');
        listItem.setAttribute('data-index', index);

        listItem.innerHTML = `
            <span class="number">${index + 1}</span>
            <div class="draggable" draggable="true"></div>
            <p class="person-name">${person}</p>
            <i class="fas fa-grip-lines"></i>
        `;

        listItems.push(listItem);
        draggable_list.appendChild(listItem);


    });


}

window.addEventListener('DOMContentLoaded', createList(), false);
<div id="draggable-list"></div>

<div id="check"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!