I’m trying to write a function that reads the oncontextmenu event for a div item. (I need it to find out whether the ctrl key was being pressed, e.g. by using e.ctrlKey.)
This works:
<div id="item_2" class="lineitem" align=center oncontextmenu="alert('foo');">test</div>
But this does not:
<script language="JavaScript">
function asdf(e){
alert('foo');
}
</script>
<div id="item_2" class="lineitem" align=center oncontextmenu=asdf>test</div>
What do I need to do to fix the second one?
Thanks.
You can’t assign predefined functions directly using attribute values. Only anonymous functions (where
function () {and}are replaced withattribute="and". This makes it rather difficult to trap the event object (maybe impossible, it isn’t something I’ve ever felt the need to investigate).If you want to assign the function directly, do it using JavaScript with attachEvent/addEventListener.