• We just launched and are currently in beta. Join us as we build and grow the community.

Remove Option In Select Tag Using JavaScript

TangerinePeel

RomCom Lover
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
116
Likes
124
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <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"

    >

    Remove Option In Select Tag Using JavaScript</

    h3

    >
  16. <hr

    style

    =

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

    /

    >

  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. <div

    class

    =

    "col-md-5"

    >
  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

    >
  35. </

    div

    >
  36. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  37. </

    body

    >
  38. </

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

    )

    ;
  3. populateSelect(

    )

    ;

  4. function

    deleteItem(

    index)

    {
  5. alert(

    "You have remove "

    +

    list[

    index]

    )

    ;
  6. list.splice

    (

    index,

    1

    )

    ;
  7. displayData(

    )

    ;
  8. populateSelect(

    )

    ;
  9. }

  10. function

    displayData(

    )

    {
  11. var

    html=

    ""

    ;
  12. for

    (

    var

    i=

    0

    ;

    i<

    list.length

    ;

    i++

    )

    {
  13. html+=

    "<tr><td>"

    +

    list[

    i]

    +

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

    +

    i+

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

    ;
  14. }

  15. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    html;
  16. }


  17. function

    populateSelect(

    )

    {
  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. }

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!


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

452,292

323,341

323,350

Top