Simple To Do List Using Bootstrap 5

Simple To Do List Using Bootstrap 5
Code Snippet:To Do List(Bootstrap5)
Author: Yan Naing
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 18,320
License: MIT
Edit Code online: View on CodePen
Read More

Yet another code snippet to create a simple to do list using Bootstrap 5. It allows users to add and remove and modify tasks lists. You can easily integrate this lightweight code to create to-do lists according to your needs.

How to Create To-Do List Using Bootstrap 5

1. In the very first step, load the Bootstrap 5 CSS into the head tag of your webpage.

    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">

2. After that, create the HTML structure for the to-do list as follows:

<div class=" container rounded-3 border border-2 border-dark my-5 bg-white" style="height:auto;">
        <div>
        <h1 class=" h1">To Do List</h1> 
    <div class="row">
            <div class=" col-8">
        <input class=" py-3 form-control shadow" placeholder="input your task" type="text" id="inputText"> 
            </div>
            <div class="col-2">
                <!-- <i onclick="addList()" class=" btn btn-dark rounded-pill fas fa-4x fa-plus-circle "></i> -->
                <button onclick="addList()" class=" mt-2 btn btn-dark"> Add </button>
            </div>
        </div>
    </div>
        <hr>
    <div class="row rounded bg-white">
        <div class=" col-12"> 
        <ul class=" list-group" id="list"></ul>
        </div> 
    </div> 
 </div>

3. Finally, add the following to-do list JavaScript program into your project and done.

let input = document.getElementById("inputText");
        let list= document.getElementById("list");
        let minimalValue = 3;
        let listNum = 0;
addList=()=>{
    // get
    let inputText = filterList(input.value);
    // set 
   if (inputText) {
    list.innerHTML += ` <li class=" my-3 py-3 shadow list-group-item " id="list${listNum}">
                <div class="row">
                <div class="col-1">
                <input class="" type="checkbox" id="check${listNum}" onclick="done(${listNum})">
                </div>
                <div class="col-6">
                    <span class=" h4" id="text${listNum}"> ${inputText} </span>
                </div>
                <div class="col-4">
                     <button class=" btn btn-dark" onclick="deleteList(${listNum})">Delete</button>
                     <button class=" btn btn-dark" onclick="editList(${listNum})">Edit</button>
                </div>                  
                 </div>    
                </li> `;
        input.value=" ";
        listNum++;

   }
}

done=(listId)=>{ 
    let checkbox = document.getElementById(`check${listId}`);
    let current = document.getElementById(`text${listId}`);
    let classExit=current.classList.contains("text-decoration-line-through");
    if (classExit == true) {
        current.classList.remove("text-decoration-line-through");
    }else{
        current.classList.add("text-decoration-line-through");
    }
    
}

filterList=(x)=>{
       if (x) {
            if (x.length >= minimalValue) {
                return x;
            }
            else{
                alert("Please enter more than 3 words")
            }
       }
       else{
            return false;
       }
}

editList=(listId)=>{
    let currentText = document.getElementById(`text${listId}`);
    let newText = prompt("Wanna Change list?",currentText.innerHTML);
    if (filterList(newText)) {
        currentText.innerHTML = newText; 
    }
}

deleteList=(listId)=>{
    let current = document.getElementById(`text${listId}`).innerHTML;
       let deleteComfirm = confirm(`Are you sure to delete ${current}`);
    if (deleteComfirm) {
         let p = document.getElementById("list")
        let c = document.getElementById(`list${listId}`);
        p.removeChild(c);
    }
    else{
        console.log("deleted");
    }
};

Hopefully, you have successfully created a to-do lists for Bootstrap 5 projects. If you have any questions or suggestions, let me know by comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X