Let me give you simplified code: you just need the
value
of the
select
element, everything else you can decide on your own, using the idea:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript"><!--
function eventHandler(event) {
alert(event.type);
alert(event.value);
}
--></script>
</head>
<body>
<select style="font-size:12pt" onChange="event.value = value; eventHandler(event)">
<option value="0">test 0</option>
<option value="1">test 1</option>
<option value="2">test 2</option>
<option value="3">test 3</option>
</select>
</body>
</html>
You can use several parameters, of course; and you do not have to use
event
object (that is a bonus :-)).
Same story with the event
onКeyPress
. The event object is more useful here: you can use
event.keyCode
and
event.which
. If you return
false
from
onКeyPress
event handler, a key press will be suppressed, which is very useful to filter out (ignore) the unwanted key presses.
[EDIT]
Answering follow-up question:
This is the simplified variant based on what you tried to do in your code. Don't say "I don't know how to use it"; this is the same as you tried to write, only simple and correct. All you need to do is replacing
alert
with the code you wanted. You wanted to get a selected value, that's it. A parameter will be taken from selected
value
, that is, 0, 1, and so on.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript"><!--
function eventHandler(value) {
alert(value);
}
--></script>
</head>
<body>
<select style="font-size:12pt" onChange="eventHandler(value)">
<option value="0">test 0</option>
<option value="1">test 1</option>
<option value="2">test 2</option>
<option value="3">test 3</option>
</select>
</body>
</html>
—SA