spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / beginning / chap6 / 5 12345
[next]
Developer News
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear

Beginning JavaScript

The Select Elements

Although they look quite different, drop-down list and list boxes are actually both elements created with the <select> tag, and strictly speaking they are both select elements. The select element has one or more options in a list that you can select from; each of these options is defined using the <option> tag. Your list of <option> tags goes in between the <select> and </select> tags.

The SIZE attribute of the <select> tag is used to specify how many of the options are visible to the user.

For example, to create a list box that is 5 rows deep and populate it with 7 options, our <select> tag would look like this:

<SELECT NAME=theDay SIZE=5> <OPTION VALUE=0 SELECTED>Monday <OPTION VALUE=1>Tuesday <OPTION VALUE=2>Wednesday <OPTION VALUE=3>Thursday <OPTION VALUE=4>Friday <OPTION VALUE=5>Saturday <OPTION VALUE=6>Sunday </SELECT>

Notice that the Monday <option> tag also contains the word SELECTED; this will make this option the default selected one when the page is loaded. The values of the options have been defined as numbers, but text would be equally valid.

If we wanted this to be a drop-down list, then we just need to change the SIZE attribute in the <select> tag to 1 and hey presto it's a drop-down list.

If we want to let the user choose more than one item from a list at once, we simply need add the MULTIPLE attribute to the <select> definition.

The <select> tag creates a Select object. This object has an options[] array property, and this array is made up of Option objects, one for each <option> element inside the <select> element associated with the Select object. For example, in the above example if the <select> element was contained in a form called theForm, with

document.theForm.theDay.options[0] we would access the option created for Monday.

How can we tell which option has been selected by the user? Easy; we use the Select object's selectedIndex property. We can use the index value returned by this property to access the selected option using the options[] array.

The Option object also has index, text, and value properties. The index property returns the index position of that option in the options[] array. The text property is what's displayed in the list and the value property is the value defined for the option, which would be posted to the server if the form were submitted.

If you want to find out how many options there are in a select element, you can use the length property of either the Select object itself or of its options[] array property.

Let's see how we could loop through the options[] array for the above select box:

var theDayElement = window.document.form1.theDay; document.write("There are " + theDayElement.length + "options"); var optionCounter; for (optionCounter = 0; optionCounter < theDayElement.length; optionCounter++) { document.write("Option text is " + theDayElement.options[optionCounter].text) document.write(" and its value is "); document.write(theDayElement.options[optionCounter].value); document.write("") }

First we set the variable theDayElement to reference the Select object. Then we write the number of options to the page, in this case 7.

Next we use a for loop to loop through the options[] array, displaying the text of each option, such as Monday, Tuesday etc., and its value, such as 0, 1 etc. If you create a page based on this code, it must be placed after the <select> tag has been defined.

It's also possible to add options to a select element after the page has finished loading. We'll look at how this is done next.

Adding New Options

To add a new option to a select element, we simply create a new Option object using the new operator, and then insert it into the options[] array of the Select object at an empty index position.

When creating a new Option object there are two parameters to pass, the first is the text you want to appear in the list, the second the value to be assigned to the option.

var myNewOption = new Option("TheText","TheValue");

We then simply assign this Option object to an empty array element, for example:

document.theForm.theSelectObject.options[0] = myNewOption;

If you want to remove an option you simply set that part of the options[] array to null. For example, to remove the element we just inserted above, we need:

document.theForm.theSelectObject.options[0] = null;

When you remove an Option object from the options[] array, the array is reordered so that the array index values of all the options above the removed one have their index value decremented by one.

When you insert a new option at a certain index position, beware that it will overwrite any Option object that is already there.

home / programming / javascript / beginning / chap6 / 5 12345
[next]

Copyright 1999 Wrox Press Ltd. and

JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 6 · Software Engineering for Ajax · Perl Pragma Primer
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Using File Virtualization for Disaster Recovery · VoIP Security: SIP—Versatile but Vulnerable · Around the World in 80 Nodes


Created: February 7, 2001
Revised: February 7, 2001


URL: http://webreference.com/programming/javascript/beginning/chap6/