I am calling a JavaScript function when an option is selected in a select element like so:
JavaScript
x
5
1
<select id="select-thingy" onchange="foo(event, this); false;">
2
<option value="bar">asdf</option>
3
4
</select>
5
The function does something like this:
JavaScript
1
12
12
1
function foo(e, elem) {
2
var thingummy = elem.options[elem.selectedIndex].value;
3
4
alert(e.ctrlKey); // for testing only
5
6
if (e.ctrlKey) {
7
// do something
8
} else {
9
// do something else
10
}
11
}
12
According to the alert, e.ctrlKey
is undefined – I thought this was supposed to return either true or false? What am I missing here?
Advertisement
Answer
As per the standard, the attribute ctrlKey
is only available on MouseEvent
s (like click
, mouseover
, etc.) but not HTMLEvent
s.