Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Form 4 | Q570 [ previous next ]

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

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;
  }
}

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.