JavaScript - Simple Remove Array Element

In this tutorial we will create a Simple Remove Array Element using JavaScript. This code will automatically remove an array value when the user click the delete button. The code use onclick() function in order to initiate a certain function that can remove an array value when you apply the array index position as a parameter in the Delete(). You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it. 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">Sourcecodster</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 - Simple Remove Array Element</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-6">
  17.                         <form action="javascript:void(0);" method="POST" class="form-group" onsubmit="Add()">
  18.                                 <label>Animal:</label>
  19.                                 <input type="text" id="animal" class="form-control"/>
  20.                                 <br />
  21.                                 <button type="submit" class="btn btn-primary form-control"><span class="glyphicon glyphicon-plus"></span> Add</button>
  22.                         </form>
  23.                 </div>
  24.                 <div class="col-md-6">
  25.                         <table class="table table-bordered">
  26.                                 <thead class="alert-info">     
  27.                                         <tr>
  28.                                                 <th>Animal</th>
  29.                                                 <th>Action</th>
  30.                                         </tr>
  31.                                 </thead>
  32.                                 <tbody id="result">
  33.                                
  34.                                 </tbody>
  35.                         </table>
  36.                 </div>
  37.         </div>
  38. </body>
  39. <script src="js/script.js"></script>
  40. </html>

Creating the Script

This code contains the script of the application. This code will remove an array value 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 el = document.getElementById('result');
  2. var animals = [];
  3.  
  4. function displayAll() {
  5.         var data = '';
  6.         if (animals.length > 0) {
  7.                 for (i = 0; i < animals.length; i++) {
  8.                         data += '<tr>';
  9.                         data += '<td>' + animals[i] + '</td>';
  10.                         data += '<td><button class="btn btn-danger" onclick="Delete(' + i + ')"><span class="glyphicon glyphicon-trash"></span> Delete</button></center></td>';
  11.                         data += '</tr>';
  12.                 }
  13.         }
  14.        
  15.         el.innerHTML = data;
  16. };
  17.  
  18.  
  19. function Add() {
  20.     var el = document.getElementById('animal')
  21.         var animal = el.value;
  22.         if (animal) {
  23.                 animals.push(animal.trim());
  24.                 el.value = '';
  25.                 displayAll();
  26.         }
  27.         displayAll();
  28. };
  29.  
  30. function Delete(item) {
  31.         animals.splice(item, 1);
  32.         displayAll();
  33. };
  34.  
  35. displayAll();
There you have it we successfully created a Simple Remove Array Element 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