Skip to main content

JavaScript - To Do List App Source Code

Language


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. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - To Do List App Source Code</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="form-inline">
  18. <label>Add List</label>
  19. <br />
  20. <input type="text" id="list" class="form-control"/>
  21. <button class="btn btn-success" onclick="addList()">ADD</button>
  22. </div>
  23. <div class="col-md-6">
  24. <h3 class="text-primary">My List</h3>
  25. <ul class="list-group" id="result" style="word-wrap:break-word;"/></ul>
  26. </div>
  27. </div>
  28. </body>
  29. <script src="js/script.js"></script>
  30. </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. function removeList(list){
  28. lists.splice(list, 1);
  29. displayList();
  30. }

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

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.