Javascript addOption - jcobban/Genealogy GitHub Wiki

UP: Javascript Global Services

Source: /jscripts/util.js

function addOption(select, text, value)

Dynamically add an Option to a Select statement.

Parameters:

parameter value
select instance of HtmlSelectElement to which the Option is added
text the text to display to the user in the selection item
value the value to pass to the server when this Option is selected

Returns: a new instance of HTMLOptionElement.

Example:

let    censusYear                = census.substring(2);
let    provSelect                = document.distForm.Province;
provSelect.options.length    = 0;   // clear the list
switch(censusYear)
{           // act on census year
    case "1831":
    {       // pre-confederation
        addOption(provSelect,   "Quebec",   "QC");
        provSelect.selectedIndex    = 0;
        break;
    }

    case "1851":
    case "1861":
    {       // pre-confederation
        addOption(provSelect,   "Canada East (Quebec)", "CE");
        addOption(provSelect,   "Canada West (Ontario)","CW");
        addOption(provSelect,   "New Brunswick",    "NB");
        addOption(provSelect,   "Nova Scotia",      "NS");
        addOption(provSelect,   "Prince Edward Island", "PI");
        provSelect.selectedIndex    = 1; // default to Canada West
        break;
    }       // pre-confederation
...

One of the reasons this function was defined is that prior to Internet Explorer release 9 the text and value attributes of a new Option could not be set until after the Option was added.

Next: function commonInit(event)