Remove Option In Select Tag Using JavaScript

In this tutorial we will create Remove Option In Select Tag using JavaScript. The program will remove any unnecessary list in the select tag dynamically. This is a free code, you are free modify this. To learn more about this, just follow the steps below.

Getting started:

First you have to download bootstrap framework, 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">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">Remove Option In Select Tag Using JavaScript</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.        
  17.                 <div class="col-md-7">
  18.                         <form>
  19.                                 <div class="form-inline">
  20.                                         <select id="data" class="form-control"></select>
  21.                                 </div>
  22.                         </form>
  23.                 </div>
  24.                
  25.                 <div class="col-md-5">
  26.                         <table class="table table-bordered">
  27.                                 <thead class="alert-info">
  28.                                         <tr>
  29.                                                 <th>Names</th>
  30.                                                 <th>Action</th>
  31.                                         </tr>
  32.                                 </thead>
  33.                                 <tbody id="result"></tbody>
  34.                         </table>
  35.                 </div>
  36.         </div>
  37. <script src="js/script.js"></script>   
  38. </body>
  39. </html>

Creating the Script

This code contains the script of the application. The code will delete an options in the select tag. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var list=["Monkey Luffy", "Uzumaki Naruto", "Jennifer Lawrence", "Black Panther", "Robbie Williams"];
  2.  
  3. displayData();
  4. populateSelect();
  5.  
  6. function deleteItem(index){
  7.         alert("You have remove " + list[index]);
  8.         list.splice(index, 1);
  9.         displayData();
  10.         populateSelect();
  11. }
  12.  
  13. function displayData(){
  14.         var html="";
  15.         for(var i=0; i<list.length; i++){
  16.                 html+="<tr><td>"+list[i]+"</td><td><button class='btn btn-danger' onclick='deleteItem("+i+")'>Delete</button></td></tr>";
  17.         }
  18.        
  19.         document.getElementById('result').innerHTML=html;
  20. }
  21.  
  22.  
  23. function populateSelect(){
  24.         var html="";
  25.        
  26.         for(var i=0; i<list.length; i++){
  27.                 html +="<option>"+list[i]+"</option>";
  28.         }
  29.        
  30.         document.getElementById('data').innerHTML=html;
  31. }
There you have it we successfully created a Remove Option In Select Tag 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