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

208
Views
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 answers
Answer question

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 Report

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 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!