I need to pass event object and event origin object to handling function. I believe I understand how external JavaScript works:
<input id="input2"/>
<script>
function getKey2(e) { alert(this.id+e.keyCode); }
document.getElementById("input2").onkeypress = getKey2;
</script>
The onkeypress method is defined on input2 DOM, so this points to it and event object is passed as the first argument to the function, but I am confused with inline JavaScript:
<input id="input1" onkeypress="getKey1(this,event)"/>
<script>
function getKey1(obj,e) {
alert(obj.id+e.keyCode);
}
</script>
The first argument, this, should be Window object, but when it is copied to obj on the event, it is event origin object, the input1 from DOM. The second argument, event, should be global event object, but when called, event object is passed to the function. Apparently, my deduction is wrong - how does the call work?
Long story short: why the object values in the following chars are not the same?
| name object value
----------------------------------
in onkeypress | this Window
in getKey1 | obj DOM input1
| name object value
----------------------------------
in onkeypress | event global event
in getKey1 | e keypress event
thisandeventwouldn't be bound correctly. It's not being executed when the DOM is parsed.