JavaScript - Remove Single Array Data

In this tutorial we will create a Remove Single Array Data using JavaScript. This code will dynamically remove a single array when the user click the delete button. The code use onclick() to call a specific function that will force to remove single array of data by setting the array index as the parameter in the deleteArray(). This is a free kind of program, feel free to modify it and use it as your 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

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="">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 - Remove Single Array Data</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <table class="table table-bordered">
  18. <thead class="alert-info">
  19. <tr>
  20. <th>Full Name</th>
  21. <th>Action</th>
  22. </tr>
  23. </thead>
  24. <tbody id="result"></tbody>
  25. </table>
  26. </div>
  27. <div class="col-md-6">
  28. <pre id="array"></pre>
  29. </div>
  30. </div>
  31. </body>
  32. <script src="js/script.js"></script>
  33. </html>

Creating the Script

This code contains the script of the application. This code will remove an array data 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 directory
  1. var members = ["Steve Roger", "Bruce Banner", "Tony Stark", "Stephen Strange", "Peter Parker", "Peter Quill"];
  3. displayTable();
  4. displayArray();
  6. function deleteArray(index){
  7. alert("You have remove " + members[index]);
  8. members.splice(index, 1);
  9. displayTable();
  10. displayArray();
  11. }
  13. function displayTable(){
  14. var table = "" ;
  16. for(var i=0; i < members.length; i++){
  17. table += "<tr>";
  18. table += "<td>" + members[i] +"</td>"
  19. + "<td><button class='btn btn-danger' onclick='deleteArray("+i+")'>Remove</button></td>";
  20. table += "</tr>";
  22. }
  24. document.getElementById("result").innerHTML = table;
  25. }
  27. function displayArray(){
  28. document.getElementById('array').innerHTML=JSON.stringify(members, null, 2);
  29. }
There you have it we successfully created a Remove Single Array Data 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