How to Update the Select Box With JavaScript

104 28
    • 1). Right-click the HTML file that contains your select box. Select "Open With," then double-click your preferred HTML editor in the program list.

    • 2). Type the following code in the body of the HTML:

      <script type="text/javascript> </script>

      Place all of your JavaScript code within these two HTML tags.

    • 3). Type the following code to update the list of options in the drop-down select box:

      var new_item = document.createElement("option");

      optn.text = "My New Value";

      optn.value = "Value";

      selectbox.options.add(new_item);

      Replace "selectbox" with the HTML ID for your drop-down select box. The second and third lines of code identify the value and text displayed in the drop-down box.

    • 4). Save the code in the editor and open the HTML page in a Web browser. Notice the new item updates on the select box.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.