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

210
Visualizações
Scripts Not Serving to Browser Window

My challenge in creating a todo's application for a JS course is to use scripts to create new elements and textContent, and then serve to browser. My scripts are creating the elements, but not displaying the text in the browser.

const todos = [{
        title: 'make bed',
        completed: true
    }, {
        title: 'shower',
        completed: false
    }, {
        title: 'shave',
        completed: true
    }, {
        title: 'feed the cat',
        completed: false
    }, {
        title: 'crush the day',
        completed: false
    }]

    const incompleteTodos = todos.filter(function (todo) {
        return !todo.completed
    })

    const summary = document.createElement('h2')
    summary.textContext = `You have ${incompleteTodos.length} todos left`
    document.querySelector('body').appendChild(summary)
    todos.forEach(function (todo) {
              
        const p = document.createElement('p')
        p.textContent = 'todo.text'
        document.querySelector('body').appendChild(p)

    })
<!DOCTYPE html>
    <html>
    <head></head>
    <body>
    <h1>Todos</h1>
    <script src ="todos-app.js"></script>
    </body>
    </html>

BrowserView + Elements

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You simply need to change your "todo.text" value to a proper value your array of todos has. In this case you are simply passing the string with the wrong value and it can be fixed by this:

const todos = [{
        title: 'make bed',
        completed: true
    }, {
        title: 'shower',
        completed: false
    }, {
        title: 'shave',
        completed: true
    }, {
        title: 'feed the cat',
        completed: false
    }, {
        title: 'crush the day',
        completed: false
    }]

    const incompleteTodos = todos.filter(function (todo) {
        return !todo.completed
    })

    const summary = document.createElement('h2')
    summary.textContext = `You have ${incompleteTodos.length} todos left`
    document.querySelector('body').appendChild(summary)
    todos.forEach(function (todo) {
              
        const p = document.createElement('p')
        p.textContent = `${todo.title}`
        document.querySelector('body').appendChild(p)

    })
<!DOCTYPE html>
    <html>
    <head></head>
    <body>
    <h1>Todos</h1>
    <script src ="todos-app.js"></script>
    </body>
    </html>

You simply need to pass todo.title instead of todo.text since that's what you have defined your value of the object as and it needs to be within template literals with placeholders ( ${} ) for the value to show up instead of a simple string literal.

Read more about them here

about 4 years ago · Juan Pablo Isaza Relatório

0

Try putting your script in the header with the defer attribute:

<head>
  <script src="todos-app.js" defer></script>
</head>

This way you are sure the entire page (including the body) exists when the script runs.

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