473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem using object methods as event handlers

Hello all.

I've a bit of a problem. I would like to use a method in an object as an
event handler. Unfortunately, when the method is called the "this" object
references the event trigger rather than my object.

For example, with the following code, an alert will be shown when the page
loads
where the value of my_name is "My Object" and nodeName is 'undefined".
Dismissing
that alert and then clicking anywhere on the page will present another alert
where the
value of my_name is "undefined" and the value of nodeName is "BODY".

<html>
<head>
<title>Event Example</title>
<script type="text/javascript" language="JavaScript">
function x( n )
{
this.my_name = n;
}
x.prototype.toString = function( level ){ return "ABC"; }
x.prototype.event_handler = function( e )
{
var evt = ( e ? e : ( window.event ? window.event : null ));
if( !evt ) return;
alert( "x.event_handler()\n" +
" my_name:\t" + this.my_name + "\n" +
" nodeName:\t" + this.nodeName + "\n" +
" event:\t\t" + evt.type );
}

function body_onload()
{
my_obj = new x( "My Object" );
my_obj.event_handler( { type:'Dummy' } );
document.body.onclick = my_obj.event_handler;
}
</script>
</head>
<body onload="body_onload();">
<input id="Button1" name="Button1" type="button" value="Button 1">
</body>
</html>

Any suggestions as to how to get around this?

Thanks in advance.,

Dave

Nov 4 '05 #1
2 1522
D. Patterson wrote:
I've a bit of a problem. I would like to use a method in an object as
an event handler.
Not at all an unusual desire.
Unfortunately, when the method is called the "this"
object references the event trigger rather than my object.

<snip>

There are a number of techniques that handle this problem. As you are
assigning the handlers as a function reference the most appropriate
method is described here:-

<URL: http://www.jibbering.com/faq/faq_notes/closures.html >

Richard.
Nov 4 '05 #2
Wow. Great article.
Now I go play for a while.
I'll post the results....later.

Thanks
Dave

"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:dk*******************@news.demon.co.uk...
D. Patterson wrote:
I've a bit of a problem. I would like to use a method in an object as
an event handler.


Not at all an unusual desire.
Unfortunately, when the method is called the "this"
object references the event trigger rather than my object.

<snip>

There are a number of techniques that handle this problem. As you are
assigning the handlers as a function reference the most appropriate
method is described here:-

<URL: http://www.jibbering.com/faq/faq_notes/closures.html >

Richard.

Nov 4 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It...
4
by: Anders | last post by:
Hi, I was wondering what is most efficient of the two. Is it more efficient to add server controls within the Itemtemplate and use OnItemDataBound to manipulate and databind the servercontrols. ...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
21
by: Mahesh Devjibhai Dhola [MVP] | last post by:
Hi, I have added few of the events in some control, example code is: btnControl.GotFocus +=new EventHandler(EventHandlingMethod); btnControl.Click +=new EventHandler(EventHandlingMethod);...
5
by: Martin Chen | last post by:
I have a frame set (as per MS FrontPage 2000). It has a contents and a main frame. The contents frame has a menu bar written with with javascript (in the context of a table). In IE6.1 everything...
1
by: harish | last post by:
hello, I am trying to write code for form activate event and for an event of an object. Generally, you select the object/form from the top combo box in visual studio ide, and select the event...
2
by: sck10 | last post by:
Hello, Is there an equivalent to using "Handles" in the codebehind for c#? Or do I have to declare every event in the GridView control like the following: <asp:GridView ID="gvSearchList"...
6
by: Joel | last post by:
2 Questions: (1) The documentation says application.run() creates a standard message loop on the current thread and "optionally" shows a form. This is really confusing because I was of the...
3
by: Ryan McFall | last post by:
Hi: I am trying to write a short program that demonstrates switching between two different event handlers when a mouse click occurs. Here's the relevant code: public delegate void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.