P
Peter Laman
I want to create an object that handles mouse events for elements,
based on context information stored in that object. This means that
the event handler must be able to access the context object. How do I
do this? Depending on how I bind the event handler, 'this' will refer
to either the HTML element or the window.
I'd like to do something like this:
In a linked .js file:
function eventContext(elementID, someContext)
{
this.element = document.getElementById(elementID)
this.someContext = someContext
this.handleClick = function(e)
{
????
}
this.element.onclick = this.handleClick
}
In the page:
<input type="button" value="Click me!!" id="myButton">
<script type="text/javascript">
new eventContext("myButton","This is the context")
</script>
In the handleClick method, I want to access the eventContext object,
but how? 'this' is the HTML element to which the event handler was
attached.
based on context information stored in that object. This means that
the event handler must be able to access the context object. How do I
do this? Depending on how I bind the event handler, 'this' will refer
to either the HTML element or the window.
I'd like to do something like this:
In a linked .js file:
function eventContext(elementID, someContext)
{
this.element = document.getElementById(elementID)
this.someContext = someContext
this.handleClick = function(e)
{
????
}
this.element.onclick = this.handleClick
}
In the page:
<input type="button" value="Click me!!" id="myButton">
<script type="text/javascript">
new eventContext("myButton","This is the context")
</script>
In the handleClick method, I want to access the eventContext object,
but how? 'this' is the HTML element to which the event handler was
attached.