Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q570 How can I dynamically add a new option to a select list?

You are here: irt.org | FAQ | JavaScript | Form | 4 | Q570 [ previous next ]

Take a look at the article Dropdown Menus #2.

The following was submitted by paulh:

The easiest method to add a new option to a form can be obtained by using the new option method. To do this declare a variable and initiate the new option object as shown

var arrThemeSubs = new Array()
var arrThemeSubsID = new Array()

arrThemeSubsID[0] = "1"
arrThemeSubsID[1] = "2"
arrThemeSubsID[2] = "3"

arrThemeSubsName[0] = "sub1"
arrThemeSubsName[1] = "sub2"
arrThemeSubsName[2] = "sub3"

function addThemeSubs() {
  for (var i=0; i < arrThemeSubs.length ;i++) {	
    var newOpt  = new Option(arrThemeSubsName[i],arrThemeSubsID[i]);
    var themesubLength = document.form.themesub.length;
    document.form.themesub.options[themesubLength] = newOpt;
  }
}

©2018 Martin Webb