Make your own free website on
.JavaScript Tutorial
 Combo Link Boxes
Go to JavaScript Start Page

  • How Combo Link Boxes look on a web page

  •  How to create combo link boxes using JavaScript
Let us see an example. If we must choose from  four cases, we can present them this way:

Using Java Script they can be written as follow:

   <form name="name_form">
         <select name="name_select" size="1">

To access anything within this selection list, we would write:

    document . name_form . name_select

Using the above list as an example, with values added in for each element:

<form name="name_form">
              <select name="name_select" size="1">
                  <option value="1">choice1</option>
                  <option value="2">choice2</option>
                  <option value="3">choice3</option>
                  <option value="4">choice4</option>

 JavaScript gives us access to the individual elements (choice1, choice 2, choice 3 etc) by using the options array. An array is a series of variables all with the same name, but with different index numbers. Lets assume we want to access the first element of this selection list:

    //gives us access to element "choice1"

This will give us direct access to the first element of the selection list. 

To access the third element, we would do this:

     //gives us access to element "choice3"

If we want to alert the name of the first element (in this case, the  name is "choice1"):

If we want to know the value that's associated with this element:

  • Properties of the options array itself

We know how to access each of the elements within the selection list, but only by hard-coding the index number into the script. For example, we still have no way of telling our script to alert the value of each element, depending on what he/she has selected. We will now proceed to learn how we can have scripts automatically update itself whenever a different selection is made by the user, and see some practical uses of this. 
ments of the options array, NOT the options array itself. 

Here are the properties we have used already:

Properties of the elements of the options array:
returns the actual text (name) of the element
returns the value of the element

Properties of the options array itself:
tells us the number of elements within this selection  list
tells us the index number of the selected option

Using the above table, to use the properties, we would do the following:


Using the length property, we can see how many elements are in the selection list:


It alerts "3", since there are three elements in the list.

The selectedIndex property informs the index number of the element selected, and updates itself whenever a new selection is made.

Make a selection: 


                 The selectedIndex property updates itself if you change elements, so if you had selected "choice1", it alerts "0" (since array indexes start at 0), if you than changed and selected "choice2", it will alert "1" instead. 

A common use of the above knowledge is to create a combo link box, where the selection list acts as a URL jumper. To access the individual elements, we would write:


where "x" is the index number of the element. 

Also recall from what we've just mentioned, that the selectedIndex property of the options array returns the index of the element that's currently selected:

Now, with the above two pieces of code, we are all set to make our combobox script! 

<form name="name_form"> 
     <select name="name_select" size="1"> 
          <option value="">choice1</option> 
          <option value="">choice2</option> 
          <option value="">choice3</option> 
<script language="javascript"> 
function name_function() 


<input type="button" name="test" value="Click Here!" onClick="name_function()"> 

Pay close attention to how we put together the two pieces of code  mentioned earlier:



    <TITLE>Combo Link Box</TITLE>
<FORM method="POST" name="name_form">
   <SELECT name="name_select" size="1">
<OPTION selected value="">Cnet</OPTION>
<OPTION value="">Geocities</OPTION>
        <SCRIPT language="javascript">
          function name_function()
  <INPUT type="button" name="name_button" value="GO!" 


To continue with Time-dependent Scripts continue