• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to Dynamically Delete HTML Select Option Item in JavaScript

guvipuzo123

Social Sales Wizard
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
50
Likes
122
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Dynamically Delete HTML Select Option Item</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-4"

    >
  18. <form

    >
  19. <div

    class

    =

    "form-inline"

    >
  20. <select

    id

    =

    "data"

    class

    =

    "form-control"

    ></

    select

    >
  21. </

    div

    >
  22. </

    form

    >
  23. </

    div

    >
  24. <div

    class

    =

    "col-md-8"

    >
  25. <table

    class

    =

    "table table-bordered"

    >
  26. <thead

    class

    =

    "alert-info"

    >
  27. <tr

    >
  28. <th

    >

    Names</

    th

    >
  29. <th

    >

    Action</

    th

    >
  30. </

    tr

    >
  31. </

    thead

    >
  32. <tbody

    id

    =

    "result"

    ></

    tbody

    >
  33. </

    table

    >
  34. </

    div

    >

    w
  35. </

    div

    >
  36. <script

    src

    =

    "script.js"

    ></

    script

    >
  37. </

    body

    >
  38. </

    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. displayData(

    )

    ;
  3. populateItem(

    )

    ;

  4. function

    displayData(

    )

    {
  5. var

    html=

    ""

    ;
  6. for

    (

    var

    i=

    0

    ;

    i<

    list.length

    ;

    i++

    )

    {
  7. html+=

    "<tr><td>"

    +

    list[

    i]

    +

    "</td><td><button class='btn btn-danger' onclick='deleteItem("

    +

    i+

    ")'>Delete</button></td></tr>"

    ;
  8. }

  9. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    html;
  10. }

  11. function

    deleteItem(

    index)

    {
  12. alert(

    "You have remove "

    +

    list[

    index]

    )

    ;
  13. list.splice

    (

    index,

    1

    )

    ;
  14. displayData(

    )

    ;
  15. populateItem(

    )

    ;
  16. }

  17. function

    populateItem(

    )

    {
  18. var

    html=

    ""

    ;

  19. for

    (

    var

    i=

    0

    ;

    i<

    list.length

    ;

    i++

    )

    {
  20. html +=

    "<option>"

    +

    list[

    i]

    +

    "</option>"

    ;
  21. }

  22. document.getElementById

    (

    'data'

    )

    .innerHTML

    =

    html;
  23. }

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:

How%20to%20Dynamically%20Delete%20HTML%20Select%20Option%20Item%20in%20JavaScript%201.png


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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,500

350,639

350,649

Top