JavaScript - To Do List App Source Code

In this tutorial we will create a To Do List App Source Code using JavaScript. This code will allow you to organize your task by submitting the form inputs. The code user onclick() function that call a specific method that will add a new list by the use of dot notation push(). This user friendly kind of code feel free to modify it and use it as you own. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - To Do List App Source Code</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="form-inline">
  17.                         <label>Add List</label>
  18.                         <br />
  19.                         <input type="text" id="list" class="form-control"/>
  20.                         <button class="btn btn-success" onclick="addList()">ADD</button>
  21.                 </div>
  22.                 <div class="col-md-6">
  23.                 <h3 class="text-primary">My List</h3>
  24.                 <ul class="list-group" id="result" style="word-wrap:break-word;"/></ul>
  25.                 </div>
  26.         </div>
  27. </body>
  28. <script src="js/script.js"></script>
  29. </html>

Creating the Script

This code contains the script of the application. This code can add your task when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var lists = []
  2.  
  3. function addList(){
  4.         var list = document.getElementById('list');
  5.         var val = list.value;
  6.         if(val == ""){
  7.                 alert("Please enter something first!");
  8.         }else{
  9.                 lists.push(val.trim());
  10.                 list.value = '';
  11.                 displayList();
  12.         }
  13.        
  14. }
  15.  
  16. function displayList(){
  17.         var data = '';
  18.         if(lists.length > 0){
  19.                 for(var i=0; i < lists.length; i++){
  20.                         data += "<li class='list-group-item'><button class='pull-right' onclick='removeList("+i+")'><span class='glyphicon glyphicon-trash'></span></button>"+lists[i]+"</li>";
  21.                 }
  22.         }
  23.        
  24.         document.getElementById('result').innerHTML = data;
  25. }
  26.  
  27.  
  28.  
  29.  
  30. function removeList(list){
  31.         lists.splice(list, 1);
  32.         displayList();
  33. }
There you have it we successfully created a To Do List App Source Code using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment