How to Add Items to a Combo Box With JavaScript

The JavaScript programming language lets you interact with users after they have already loaded the page into the Web browser. This language is used after your pages have already loaded the dynamic content from the server. You can insert new combo box values using JavaScript. Combo boxes are the drop-down elements used to prompt the user for a selection. Website owners use combo boxes to list only a specific number of answers.

Instructions

    • 1

      Right-click the HTML file on your desktop and select "Open With." Choose "Notepad" from the list of programs and click "OK." This lets you use the free text editor in Windows, but you can also choose a third-party, purchased application if you have one installed.

    • 2

      Type the JavaScript script block within the <head> tags. The JavaScript block is used to designate an area of JavaScript code. Type the following code within the <head> and </head> tags:

      <script language="Javascript"> </script>

    • 3

      Type the following code into the script block you created in Step two:

      var value= combobox.appendChild(document.createElement('option'));

      value.text = "New Combo Box Selection";

      The first line of code creates the New Option object. This creates the option in the combo box named "combobox." Replace this name with your own combo box name. The "value.text" line item sets the text seen by the user. In this example, the user now has the option to select "New Combo Box Selection" from the combo box.

    • 4

      Click the "File" menu and select "Save" to save your changes. Double-click the HTML file to open it in your browser. Notice that the new option is loaded into the combo box.

Related Searches:

References

Comments

Related Ads

Featured