Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

317
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda