Tuesday, December 27, 2005

Adding and Removing items from an Html ListBox

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/adding_and_removing_items_from_an_html_listbox.htm]

ASP.Net makes things easy, like adding and removing items from an HTML listbox. The problem is that these items are stored as inner nodes... it's not just as simple as setting a value property. Therefore you modify items by modifying the DOM itself. Here's an Html page to do that. It gives each item a description and id (for lookup purposes). The methods that really matter (i.e. reuse them into your own code) are AddItem and RemoveItem. Both take a listBox object, and id, and AddItem also takes the text to display.

Note that in pure html, both the ListBox and Dropdown are created from the