TODOS List using JavaScript

 <!doctype html>

<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">

    <title>TODOs LIST</title>
</head>

<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">TODOs LIST</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <a class="nav-link active" aria-current="page" href="#">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">About</a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            Items
                        </a>
                        <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <li><a class="dropdown-item" href="#">Action</a></li>
                            <li><a class="dropdown-item" href="#">Another action</a></li>
                            <li>
                                <hr class="dropdown-divider">
                            </li>
                            <li><a class="dropdown-item" href="#">Something else here</a></li>
                        </ul>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Contact</a>
                    </li>
                </ul>
                <form class="d-flex">
                    <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
                    <button class="btn btn-outline-success" type="submit">Search</button>
                </form>
            </div>
        </div>
    </nav>
    <div class="container my-4">
        <h2 class="text-center">TODOs List</h2>

        <div class="mb-3">
            <label for="exampleInputEmail1" class="form-label">
        <Title></Title>
      </label>
            <input type="email" class="form-control" id="title" aria-describedby="emailHelp">
            <div id="emailHelp" class="form-text">Add an item to the list</div>
        </div>

        <div class="mb-3">
            <label for="description" class="form-label">Description</label>
            <textarea class="form-control" id="description" rows="3"></textarea>
        </div>

        <button id="add" class="btn btn-primary">Add to List</button>
        <button id="clear" class="btn btn-sm btn-primary" onclick="clearStorage()">Clear List</button>

        <div id="items" class="my-4">
            <h2>Your Items</h2>
            <table class="table">
                <thead>
                    <tr>
                        <th scope="col">Srno:</th>
                        <th scope="col">Item Title</th>
                        <th scope="col">Item Description</th>
                        <th scope="col">Ations</th>

                    </tr>
                </thead>
                <tbody id="tableBody">
                    <tr>
                        <th scope="row">1</th>
                        <td>get some coffee</td>
                        <td>You need coffee you are as a coder</td>
                        <td><button class="btn btn-sm btn-primary">Delete</button></td>
                    </tr>

                </tbody>
            </table>
        </div>
    </div>

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>

    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.1/dist/umd/popper.min.js" integrity="sha384-SR1sx49pcuLnqZUnnPwx6FCym0wLsk5JZuNx2bPPENzswTNFaQU1RDvt3wT4gWFG" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.min.js" integrity="sha384-j0CNLUeiqtyaRmlzUHCPZ+Gy5fQu0dQ6eZ/xAww941Ai1SxSY+0EQqNXNE6DZiVc" crossorigin="anonymous"></script>
    -->
    <script>
        function getAndUpdate() {
            console.log("Updating List......")
            tit = document.getElementById('title').value;
            desc = document.getElementById('description').value;
            if (localStorage.getItem('itemsJson') == null) {

                itemJsonArray = [];
                itemJsonArray.push([titdesc]);
                localStorage.setItem('itemsJson'JSON.stringify(itemJsonArray))
            } else {
                itemJsonArrayStr = localStorage.getItem('itemsJson')
                itemJsonArray = JSON.parse(itemJsonArrayStr);
                itemJsonArray.push([titdesc]);
                localStorage.setItem('itemsJson'JSON.stringify(itemJsonArray))
            }
            update()

        }

        function update() {
            if (localStorage.getItem('itemsJson') == null) {

                itemJsonArray = [];

                localStorage.setItem('itemsJson'JSON.stringify(itemJsonArray))
            } else {
                itemJsonArrayStr = localStorage.getItem('itemsJson')
                itemJsonArray = JSON.parse(itemJsonArrayStr);

            }
            //populate the table
            let tableBody = document.getElementById('tableBody')
            let str = "";
            itemJsonArray.forEach((elementindex=> {
                str += `
                <tr>
                  <th scope="row">${index + 1}</th>
                  <td>${element[0]}</td>
                  <td>${element[1]}</td>
                  <td><button class="btn btn-sm btn-primary" onclick="deleted(${index})"}>Delete</button></td>
              </tr>`;
            });
            tableBody.innerHTML = str;
        }
        add = document.getElementById("add");
        add.addEventListener("click"getAndUpdate);
        update()

        function deleted(itemIndex) {
            console.log("delete"itemIndex);
            itemJsonArrayStr = localStorage.getItem('itemsJson')
            itemJsonArray = JSON.parse(itemJsonArrayStr);
            //delete item index element from the array
            itemJsonArray.splice(itemIndex1);
            localStorage.setItem('itemsJson'JSON.stringify(itemJsonArray))
            update();
        }

        function clearStorage() {
            if (confirm("Do You realy want to clear?")) {
                console.log('clearing the storage')
                localStorage.clear();
                update()
            }
        }
    </script>
</body>

</html>




Comments

Popular Posts