JavaScript Tutorial JavaScript Examples JQuery Tutorial CSS Tutorial HTML Tutorial About Us

Lesson 21: Selection List

A selection list lets users choose an item from a list of items. For example, online shopping malls such as or auction sites such as let you select categories of products from a drop-down list. In JavaScript, you can create a selection list easily using the <SELECT >tag. The <SELECT> tag creates a Listbox and the <OPTION> tags create the items inside the Listbox. The number of <OPTION> tags is unlimited. The close tag for the Listbox is </SELECT>

The Listbox can display many items together with a scroll bar. The number of items shown is specified by the SIZE attribute. If the SIZE attribute is left out, the Listbox will be displayed as a dropdown box, where only one item is shown and the user needs to click to see more items.

Example 21.1: A listbox that displays all the items at once

This JavaScript creates a listbox that displays all the items at once.




<OPTION>Tablet Computers
<OPTION>Mobile Phones


The Output

In this example, you will see a drop-down Listbox which displays only one item. You need to click on the small arrow on the right of the Listbox to view other items.

Example 21.2: A drop-down Listbox


<OPTION>Tablet Computers
<OPTION>Mobile Phones


The output

To check which item is selected in the listbox, we can use the selectedIndex property of the listbox object.  Besides that, we retreive the content of the list box using the getElementById method using the id="list1". In addition, we use the options method to display the text of the selected item.

Example 21.3


function testItem() {

var myselect = document.getElementById("list1");
var myitem = myselect.options[myselect.selectedIndex].text;
alert(myitem+" is selected");



You also need to Create the following selection box.

<select id="list1" SIZE="5">
<option>Tablet Computer


<input Value="Check Item" onClick"testItem()">

The Output

The output is shown below and you can click on the Check Item button to test. Here is another good working example using the options button in JavaScript. You can select a certain item then click OK, it will bring you to the selected page.

Example 21.4: Books Selection ListBox

The Code
//created by Liew Voon Kiong April 24 1998 All Rights Reserved.

function loadHtml(form){
if (form.list.selectedIndex==0) location="art.html"
if (form.list.selectedIndex==1) location="astro.html"
if (form.list.selectedIndex==2) location="bestsell.html"
if (form.list.selectedIndex==3) location="business.html"
if (form.list.selectedIndex==4) location="children.html"
if (form.list.selectedIndex==5) location="computer.html"
if (form.list.selectedIndex==6) location="health.html"
if (form.list.selectedIndex==7) location="mystery.html"
if (form.list.selectedIndex==8) location="horror.html"
if (form.list.selectedIndex==9) location="nonfic.html"
if (form.list.selectedIndex==10) location="romance.html"
if (form.list.selectedIndex==11) location="science.html"
if (form.list.selectedIndex==12) location="scific.html"
if (form.list.selectedIndex==13) location="self.html"
if (form.list.selectedIndex==14) location="sports.html"
if (form.list.selectedIndex==15) location="travel.html"
if (form.list.selectedIndex==16) location="young.html"
if (form.list.selectedIndex==17) location="music.html"



<SELECT size="4">
<OPTION>Art and Drawing
<OPTION>Best Sellers
<OPTION>Business & Investment
<OPTION>Children Books
<OPTION selected>Computer & Internet
<OPTION>Health & Fitness
<OPTION>Horror & Thriller
<OPTION>Non Fiction
<OPTION>Science & Nature
<OPTION>Science Fiction
<OPTION>Self Improvement
<OPTION>Sports & Games
<OPTION>Young Readers' Corner
</SELECT><INPUT Value="OK"><font size="2">

The Output
Search for your books by selecting a category, then click OK .

❮ Previous Lesson Next Lesson ❯

Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy