How to Dynamically Delete HTML Select Option Item in JavaScript

How to Dynamically Delete HTML Select Option Item in JavaScript

Introduction

In this tutorial we will create a How to Dynamically Delete HTML Select Option Item in JavaScript. This tutorial purpose is to show you how you can delete a select tag items. This will tackle all the important functionality that immediately remove select item when the button is clicked. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want to remove an item in the select option. I will give my best to provide you the easiest way of creating this program Delete Select Option Item. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will HTML input select and buttons. To create this simply copy and write it into your text editor, then save it 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">How to Dynamically Delete HTML Select Option Item</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-4">
  17. <form>
  18. <div class="form-inline">
  19. <select id="data" class="form-control"></select>
  20. </div>
  21. </form>
  22. </div>
  23. <div class="col-md-8">
  24. <table class="table table-bordered">
  25. <thead class="alert-info">
  26. <tr>
  27. <th>Names</th>
  28. <th>Action</th>
  29. </tr>
  30. </thead>
  31. <tbody id="result"></tbody>
  32. </table>
  33. </div>w
  34. </div>
  35. <script src="script.js"></script>
  36. </body>
  37. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically remove an item in the select option when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var list=["John Smith", "Claire Temple", "Pedro Suwan", "Eren Nirtch", "Henry Calvon"];
  2.  
  3. displayData();
  4. populateItem();
  5.  
  6. function displayData(){
  7. var html="";
  8. for(var i=0; i<list.length; i++){
  9. html+="<tr><td>"+list[i]+"</td><td><button class='btn btn-danger' onclick='deleteItem("+i+")'>Delete</button></td></tr>";
  10. }
  11.  
  12. document.getElementById('result').innerHTML=html;
  13. }
  14.  
  15. function deleteItem(index){
  16. alert("You have remove " + list[index]);
  17. list.splice(index, 1);
  18. displayData();
  19. populateItem();
  20. }
  21.  
  22. function populateItem(){
  23. var html="";
  24.  
  25. for(var i=0; i<list.length; i++){
  26. html +="<option>"+list[i]+"</option>";
  27. }
  28.  
  29. document.getElementById('data').innerHTML=html;
  30. }

In the code above we will only create one method called deleteItem(). this function will dynamically remove an item in the select option. This code uses a special function called splice(), this will remove the selected item base on the index position.

Output:

The How to Dynamically Delete HTML Select Option Item in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Dynamically Delete HTML Select Option Item in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment