PDA

View Full Version : help with onClick event


zduck
12-23-2002, 07:16 PM
I'm trying to use an onClick event with an option element. Like so...

<select name="name">
<option value="value">one</option>
<option value="value" onClick="whatever">two</option>
</select>

I know that what I type into the onClick event is correct because it works just fine in the onClick event of a button. Any suggestions?

chrismiceli
12-23-2002, 07:43 PM
try onFocus="whatever"

PauletteB
12-24-2002, 12:56 AM
The basic structure should be something like

With a click button:
<form name="theform">
<select name="thename">
<option value="value1.html">one</option>
<option value="value2.html">two</option>
</select>
<input type="button" value="Go" onClick="whatever()">
</form>

Without a click button:
<form name="theform">
<select name="thename" onChange="whatever()">
<option selected value="javascript:void(0)">Select...</option>
<option value="value1.html">one</option>
<option value="value2.html">two</option>
</select>
</form>

whammy
12-24-2002, 01:01 AM
Paulette is correct, since you cannot use an onclick event with a select dropdown. Usually you would want to use onchange, however you can also use onfocus depending on the circumstances.

If you're trying to determine which option was selected, you should use onchange and write a function for it, i.e.:

<select name="blah" onchange="myfunction(this.selectedIndex)">

and in your <head> section you can have a function that does something:

<script type="text/javascript">
<!--
function myfunction(whatever) { // "whatever" is this.selectedIndex, passed to the function
if (whatever == 1) { // don't forget that dropdowns are indexed starting with 0!
// do something
}
}
// -->
</script>


Hope this helps! :)