If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

using javascript in a drop down menu

hi I have an assignment for class where we are supposed to create a drop down list and when a selection is made from the list, execute a function. The function will decide if which item was selected in the drop down list and will change the image to that respective image and display a unique pop up alert message.
So far I got it so when you click on any of the list, the first decision where the first image(which is already there when the page loads) displays and the first alert message comes up, this happens when you select any of the options on the list. I got to figure out how to get the other selections on the list to work, is there something wrong with my global variable? Here is my code-

First the onclick event isn't standard for the <option> tag, so always use onchange on the <select> tag.
Your global variable is unnecessary.

Code:

<select onchange = "changeSun( this.selectedIndex )" >

The selectedIndex property means you don't have to hard code choices into a switch-case, so you can lose it in favour of arranging your messages as an array, which can be indexed by the parameter sent to the function. The parameter can also be used to build your image filename.

Where used, return should be executed unconditionally and always as the last statement in the function.

thanks!

Thanks! that is a lot of great information and I'm appreciative of your response. The assignment asks that we use a switch construct though so is that code applicable to the code using a switch construct?