Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript some kind of error in my code

3lyfox

Coder
thats my html of it --

HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Task Board</title>
    <link rel="stylesheet" href="style.css">
</head>
    <body>
        <div class="h1">
            My Task Board
        </div>
        <div class="AT">
            <h2>New Task</h2>
            <p>
                <label><b>Task Info</b></label>
                <input type="text" id="tid" placeholder="Task Details" required>
            </p>
            <p>
                <label><b>Date</b></label>
                <input type="date" id="tad" required>
            </p>
            <p>
                <label><b>Time</b></label>
                <input type="Time" id="tti" required>
            </p>
            <p>
                <button class="btn" onclick="addTask()">Add Task</button>
            </p>
        </div>
        <div class="h3">
            <h3>
                Tasks
            </h3>
            <table border="1">
                <thead>
                    <th>Task</th>
                    <th>Task Time</th>
                    <th>Task Date</th>
                </thead>
                <tbody id="tableBody"></tbody>
             </table>
        </div>
        <script src="script.js"></script>
        <script src="Task.js"></script>
    </body>
</html>

and thats my JS of it--

JavaScript:
const tableBody = document.getElementById("tableBody")
const tid = document.getElementById("tid");
const tad = document.getElementById("tad");
const tti = document.getElementById("tti");

if (localStorage.savedTaskList)

    TaskList = JSON.parse(localStorage.savedTaskList);

    displayTable();

function displayTable() {

    tableBody.innerHTML = "";

    for (let i = 0; i < taskList.length; i++) {
        TableBody.innerHTML += `
        <tr>
            <td>${task.tid}<td>
            <td>${task.tad}<td>
            <td>${task.tti}<td>
        </tr>
        `;
    }
}

function addTask() {
    let newTask = new Task(tid.value, tad.value, tti.value);
    TaskList.push(newTask);
    localStorage.savedTaskList = JSON.stringify(taskList);
    displayTable();
}

can you please help me figure out the solution / fix please
 
Last edited by a moderator:
from what i know the script is supposed to take the info i put in the html and display / add it on screen as a table but it keeps saying i have an error that something isnt defined or / missing
 
Here:
JavaScript:
tableBody.innerHTML = "";

    for (let i = 0; i < taskList.length; i++) {
        TableBody.innerHTML += `

First you define the variable as 'tableBody' and then you write 'TableBody', so you made a mistake on its name.

Next time, give us the error message please ;)
 
tried to make that change still no change in the console it says "uncaught reference Error taskList is not defined" (in line 13)
and when i try to run it anyways it says same thing in line 25 with Task not being defined
 
Pretty much the same thing. You defined a variable named 'TaskList':
JavaScript:
TaskList = JSON.parse(localStorage.savedTaskList);
But later on, you use it as 'taskList.':
JavaScript:
for (let i = 0; i < taskList.length; i++) {

Note that the following:
JavaScript:
if (localStorage.savedTaskList)

    TaskList = JSON.parse(localStorage.savedTaskList);

    displayTable();
is equivalent to:
JavaScript:
if (localStorage.savedTaskList)
{
    TaskList = JSON.parse(localStorage.savedTaskList);
}
    
    displayTable();
Javascript does not work like Python.

Also, when you define a variable, you must use either 'var' or 'let'. So instead of:
JavaScript:
TaskList = JSON.parse(localStorage.savedTaskList);
Write the following:
JavaScript:
var taskList = JSON.parse(localStorage.savedTaskList);
 
thx for the help but i figured an "easier" solution took me some time but thats pretty much what i did
const tableBody = document.getElementById("tableBody"); const tid = document.getElementById("tid"); const tad = document.getElementById("tad"); const tti = document.getElementById("tti"); const btn = document.getElementById("btn") const taskList = document.getElementById("taskList") btn.onclick = function () { const task = tid.value const date = tad.value const time = tti.value if (task && date && time) { localStorage.setItem(task, date, time); location.reload(); } }; for (let i = 0; i < localStorage.length; i++) { const tid = localStorage.key(i); const tad = localStorage.getItem(tid); const tti = localStorage.getItem(tad); taskList.innerHTML += `${tid}, ${tad}, ${tti} </br>`; }
(+ realised i left some things out of the script which made me wonder a bit)
 
Back
Top Bottom