Home > Back-end >  How to prevent content(Tasks) to disappear after REFRESHING
How to prevent content(Tasks) to disappear after REFRESHING

Time:06-18

I am trying to make a simple To-Do like application. I want the "Tasks" which I add to remain even after closing the window or at least after refreshing. I tried to achieve that using MongoDB but I couldn't.
I think a simpler way to achieve this is by using arrays but I am not able to understand how to do that.
Please suggest me some way to achieve the above results....

let div = document.getElementById('tasks');
let add = document.getElementById('add');
let newTask = document.getElementById('newTask');

let c = 0;

add.addEventListener("click", function () {
    let newt = newTask.value;
    newTask.value = null;
    let cbox = document.createElement("input");
    let br = document.createElement("br");
    cbox.type = "checkbox";
    cbox.id = "check";
    let d = document.createElement("div");
    div.append(d);
    d.id = "nd";
    d.append(cbox);
    d.append(newt);
    c  = 1;
    if (c === 12) {
        add.disabled = true;
        newTask.value = "Stack Overflow";
    }
});
/* body {
    background-color: #42f5f5
} */

.card {
    margin: 0 auto;
    float: none;
    /* margin-bottom: 10px; */
    /* border: none; */
    /* background-color: rgb(248, 179, 88); */
}

.container {
    margin-top: 2.5rem;
}

#title {
    width: 200px;
    border: none;
    font-size: larger;
    font-weight: bold;
}

#newTask {
    width: 200px;
}

#add {
    margin-top: 1rem;
}

#newTask {
    margin-top: 1rem;
}

#plus {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 15px;
}

#nd {
    font-size: 20px;
    margin-top: 5px;
    margin-bottom: 10px;
}

#check {
    margin-left: 20px;
    height: 15px;
    width: 15px;
    margin-right: 10px;
}
<!doctype html>
<html lang="en">

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
            integrity="sha384-Gn5384xqQ1aoWXA 058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <link href="ToDo.css" rel="stylesheet">
        <title>To-Do</title>
    </head>

    <body>

        <div >
            <div  style="width: 35rem;height: 42rem;">
                <div >
                    <p style="font-size: 2rem;font-weight: bold;">
                        <input type="text"  id="title" placeholder="Title">
                    </p>
                    <p id="main">
                    <div id="tasks">

                    </div>
                    <form >
                        <div >
                            <input type="text"  id="newTask" autocomplete="off"
                                placeholder="Enter new task">
                        </div>
                        <button type="button"  id="add">Add Task</button>
                    </form>
                    </p>
                </div>
            </div>
        </div>

        <script src="ToDo.js"></script>
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
            integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
            crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
            integrity="sha384-ApNbgh9B Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
            crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
            integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5 76PVCmYl"
            crossorigin="anonymous"></script>
    </body>

</html>

CodePudding user response:

You can use localStorage to save data in browser storage.
here's link to documentation for localStorage .

//to set data in local storage
localStorage.setItem('myCat', 'Tom')

//to get data from localStorage
const cat = localStorage.getItem('myCat');

//to remove data from localStorage

localStorage.removeItem('myCat');


//for object you can stringify them

var myObj = [{name:"test", time:"Date 2017-02-03T08:38:04.449Z"}];
localStorage.setItem('item', JSON.stringify(myObj));

//Then when you want to retrieve data, you need to parse the string to object again.

var getObj = JSON.parse(localStorage.getItem('item'));


  • Related