How to Insert new Option in Select Tag Dynamically in JavaScript

How to Insert new Option in Select Tag Dynamically in JavaScript

Introduction

In this tutorial we will create a How to Insert new Option in Select Tag Dynamically in JavaScript. This tutorial purpose is to teach you to insert new option in select tag. This will cover all the basic function that will insert new options. 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 that use selection tag as your choices. I will give my best to provide you the easiest way of creating this program Insert new Option in Select tag. So let's do the coding.

Before we get started:

This 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 display the html form with and button. 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 Insert new Option in Select Tag Dynamically</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-4">
  17.                         <div class="form-inline">
  18.                                 <label>Enter your option</label>
  19.                                 <input type="text" id="option" class="form-control"/>
  20.                         </div>
  21.                                 <br />
  22.                                 <button type="button" class="btn btn-primary btn-block" onclick="createOption()">Add Option</button>
  23.                                 <br />
  24.                         <div class="form-group">
  25.                                 <label>Select an option</label>
  26.                                 <select id="select" class="form-control"></select>
  27.                         </div>
  28.                         <button class="btn btn-success btn-block" onclick="submitData();">Submit</button>
  29.                 </div>
  30.         </div>
  31. <script src="script.js"></script>
  32. </body>
  33. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow you to add new options in select tag dynamically. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function createOption(){
  2.         let select = document.getElementById('select');
  3.         let option = document.getElementById('option');
  4.        
  5.         if(option.value == ""){
  6.                 alert("Please enter something first!");
  7.         }else{
  8.                 let newOption = document.createElement("option");
  9.                 let newOptionValue = document.createTextNode(option.value);
  10.                
  11.                 newOption.appendChild(newOptionValue);
  12.                 select.insertBefore(newOption, select.lastChildNode);
  13.                
  14.                 option.value = "";
  15.                 select.value = "";
  16.         }
  17. }
  18.  
  19. function submitData(){
  20.         let data = document.getElementById('select').value;
  21.        
  22.         if(data==""){
  23.                 alert('Please select an item first');
  24.         }else{
  25.                 alert('You select '+data+'');
  26.         }
  27. }

In the code above we will a create a method called createOption. This function is the one the will create a new options for your select tag. This method use a a function that will create a new text called createTextNode(). Then it will now append the newly created text into the select options using the appendChild() and insertBefore() function.

Output:

The How to Insert new Option in Select Tag Dynamically 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 Insert new Option in Select Tag Dynamically 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