A Simple To-Do List In Javascript

Introduction

 
In this article, we discuss how to create a simple To-Do List in JavaScript like this: 
 
To-Do-List1.jpg
 
Step 1
 
First, we will create a TextBox, Add and Delete Buttons and a table Like this:
  1. <h1>To-Do List</h1>  
  2.  <input type="textbox" id="txt1" />  
  3.     <input type="button" value="Add List" onclick="addList('ToDoListTable')" />  
  4.     <input type="button" value="Delete List" onclick="deleteList('ToDoListTable')" />  
  5.      <table id="ToDoListTable" width="350px" border="1">  
  6.         </table>  
  7. Total Work pending <a id="pTotal"></a>  = 
Step 2
 
Now we will write the code, by which we can dynamically add the Rows in our Table. So when we click on the Add List Button, a new row will be dynamically generated with a TextBox and a CheckBox Control. For this we will write the code on the click event of the Add Button Like this:
  1. function addList(myTable) {  
  2.    
  3.             var table = document.getElementById(myTable);  
  4.             var rowCount = table.rows.length;  
  5.             var row = table.insertRow(rowCount);  
  6.    
  7.   
  8.           var b=document.getElementById('txt1').value;  
  9.  var cell1 = row.insertCell(0);  
  10.             var element1 = document.createElement("input");  
  11.             element1.type = "text";  
  12.             element1.style.width="100%";  
  13.             element1.name = "txtbox[]";  
  14.             element1.value=b;  
  15.                    cell1.appendChild(element1);  
  16.  var cell2 = row.insertCell(1);  
  17.             var element2 = document.createElement("input");  
  18.             element2.type = "checkbox";  
  19.             element2.name="chkbox[]";  
  20.             cell2.appendChild(element2);  
  21. document.getElementById('txt1').value=" ";  
  22. document.getElementById('pTotal').innerHTML= rowCount + 1;  
  23.        } 
Here we create two elements, a TextBox and a CheckBox and insert it into the Table cell.
 
Now look at this code:
  1. document.getElementById('pTotal').innerHTML= rowCount + 1; 
It will automatically increase the number that shows the number of pending work like this:

To-Do-List2.jpg
 
Step 3
 
Now we will write the code to delete the items in the list, in order to delete the task we will click on the CheckBox, so when we click on the CheckBox and then click on the Delete List Button the task will be deleted. For this, we will write the code in the Delete List Button:
  1. function deleteList(myTable) {  
  2.             var table = document.getElementById(myTable);  
  3.             var rowCount = table.rows.length;  
  4.    
  5.             for(var i=0; i<rowCount; i++) {  
  6.                 var row = table.rows[i];  
  7.                 var chkbox = row.cells[1].childNodes[0];  
  8.                 if(null != chkbox && true == chkbox.checked) {  
  9.                     table.deleteRow(i);  
  10.                     rowCount--;  
  11.                     i--;  
  12.  document.getElementById('pTotal').innerHTML= rowCount;  
  13.    
  14.             }  
  15.             }  
  16.         } 
Output
 
To-Do-List3.jpg
 
When we click on the Delete Button the output will be:
 
To-Do-List4.jpg


Similar Articles