Abdul Basit Mehtab
Abdul Basit Mehtab

Reputation: 23

How to resolve an error in To Do List Web App

Currently experimenting with JavaScript and building a To-Do List Web App. Having some issues with it. When I refresh the page, it gives the error:

TypeError: tasks is null

And when I press the submit button, it gives the error:

TypeError: cyclic object value

I was trying it out after reading a tutorial online on JavaScript and then I got the idea on implementing it with some basic project like this one. How can I resolve the errors? I can't seem to figure out the solution.

JS Code:

document.getElementById('form-Task').addEventListener('submit', saveTask);

// Save new To-Do Item
function saveTask(e){

let title = document.getElementById('title').value;
let description = document.getElementById('description').value;

let task = {
    title,
    description
};

if(localStorage.getItem('tasks') === null) {
    let = tasks = [];
    tasks.push(tasks);
    localStorage.setItem('tasks', JSON.stringify(tasks));
} else {
    let tasks = JSON.parse(localStorage.getItem('tasks'));
    tasks.push(task);
    localStorage.setItem('tasks', JSON.stringify(tasks));
}

getTasks();

// Reset form-Task
document.getElementById('form-Task').reset();
e.preventDefault();
}




// Delete To-Do Item
function deleteTask(title) {

let tasks = JSON.parse(localStorage.getItem('tasks'));
for (let i = 0; i < tasks.length; i++){
    if(tasks[i].title == title) {
        tasks.splice(i, 1);
    }
}

localStorage.setItem('tasks', JSON.stringify(tasks));
getTasks();
}




// Show To-Do List
function getTasks(){

let tasks = JSON.parse(localStorage.getItem('tasks'));
let tasksView = document.getElementById('tasks');
tasksView.innerHTML = '';

for(let i = 0; i < tasks.length; i++){
    let title = tasks[i].title;
    let description = tasks[i].description;

    tasksView.innerHTML +=
    `<div class="card mb-3">
        <div class="card-body">
            <div class="row">
                <div class="col-sm-3 text-left">
                    <p>${title}</p>
                </div>
                <div class="col-sm-7 text-left">
                    <p>${description}</p>
                </div>
                <div class="col-sm-2 text-right">
                    <a href="#" onclick="deleteTask('${title}')" class="btn btn-danger ml-5">X</a>             
                </div>
            </div>
        </div>
    </div>`;
}
}

getTasks();

HTML code:

<nav class="navbar navbar-light bg-light">
    <div class="container">
        <a class="navbar-brand" href="#">My To-Do List</a>
    </div>
</nav>

<div class="container">
    <div class="row my-5">
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <form id="form-Task">
                        <div class="form-group">
                            <input type="text" id="title" class="form-control" maxlength="50" autocomplete="off" placeholder="Title" required>
                        </div>
                        <div class="form-group">
                            <textarea type="text" id="description" cols="30" rows="10" class="form-control" maxlength="500" autocomplete="off" placeholder="Description" required></textarea>
                        </div>
                        <button type="submit" class="btn btn-success btn-block">Save</button>
                    </form>
                </div>

            </div>
        </div>

        <div class="col-md-8">
            <div class="row">
                <div class="col-sm-3 text-left">
                    <p class="font-weight-bold">Title</p>
                </div>
                <div class="col-sm-6 text-left">
                    <p class="font-weight-bold">Description</p>
                </div>
                <div class="col-sm-3 text-right">
                    <p class="font-weight-bold">Delete</p>
                </div>
            </div>
            <hr>
            <div id="tasks"></div>

        </div>

    </div>

</div>

<script src="js/app.js"></script>

Upvotes: 0

Views: 153

Answers (1)

Cortoloman
Cortoloman

Reputation: 715

I am still learning javascript so I might have missed something, but in your first if statement, you have an extra "=":

if(localStorage.getItem('tasks') === null) {
    let = tasks = []; // let tasks = []
    tasks.push(tasks);

and I am not sure about that first definition of the task variable. With curly braces, you make key-value pairs, but you didn't add the value. For example:

var person = { firstName: "John", lastName: "Doe" };

Hope I helped.

Upvotes: 1

Related Questions