473,395 Members | 1,442 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,395 software developers and data experts.

adding an object method as an event listener

Eli
Hello,

****************
function myClass(name) {
this.name=name;
}
myClass.prototype.printName=function(evt) {
try {
if (evt.which==1 || evt.button==1)
document.write('Mr '+this.name);
}
catch (e) {
document.write('Sorry, what is your name?');
}
}
var myObj=myClass('Jhons');
var clickme=document.getElementById('clickme');
clickme.addEvent(clickme,'mousedown',myObj.printNa me);
****************

When clicking, I get always: "Sorry, what is your name?"..
Consider myObj is not a singleton, and I'm adding the same function to
many elements as event listeners.
How can I get the expected name, when used as event handler?

-thanks, Eli

Sep 5 '06 #1
3 1138

Eli написав:
Hello,

****************
function myClass(name) {
this.name=name;
}
myClass.prototype.printName=function(evt) {
try {
if (evt.which==1 || evt.button==1)
document.write('Mr '+this.name);
}
catch (e) {
document.write('Sorry, what is your name?');
}
}
Why try/catch, not just "else"?
var myObj=myClass('Jhons');
var myObj=new myClass('Jhons');
var clickme=document.getElementById('clickme');
clickme.addEvent(clickme,'mousedown',myObj.printNa me);
Maybe "attachEvent", not an "addEvent"?
Try also
clickme.onmousedown=myObj.printName

Regards,
Val Polyakh

Sep 5 '06 #2
Eli

sc********@gmail.com wrote:
var clickme=document.getElementById('clickme');
clickme.addEvent(clickme,'mousedown',myObj.printNa me);
Maybe "attachEvent", not an "addEvent"?
addEvent is a wrapper for IE and Mozilla to attach event listeners.

*********
function addEvent(obj,e_type,func) {
if (obj.addEventListener) {
obj.addEventListener(e_type,func,false);
return true;
}
else if (obj.attachEvent) {
var ret=obj.attachEvent('on'+e_type,func);
return ret;
}
return false;
}
function removeEvent(obj,e_type,func) {
if (obj.removeEventListener) {
obj.removeEventListener(e_type,func,false);
return true;
}
else if (obj.detachEvent) {
obj.detachEvent('on'+e_type,func);
return true;
}
return false;
}
*********
Try also
clickme.onmousedown=myObj.printName
In this case, 'this' points to the object itself which is what I want,
but the event object is not transferred to the function.

-thanks, Eli

Sep 5 '06 #3
Eli
OK.. already solved it.. :-)

-thanks, Eli

Sep 6 '06 #4

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

Similar topics

6
by: Joe Kelsey | last post by:
When you use addEventListener (or addEvent in IE) to call an object method, does it call it with the correct this parameter? The ECMAScript reference has a lot to say about the caller using...
6
by: b.dam | last post by:
I'm trying to do the following: function row() { this.selected = false; this._el = document.createElement("TR"); this._el.attachEvent("onClick", this.rowClick); } row.prototype.rowClick =...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
1
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
6
by: Jeremy | last post by:
I want each instance of an object to be able to listen for input events. When the event occurs, a method of the object should be called, such that "this" is in scope and refers to the object...
2
by: darthghandi | last post by:
I am creating a listener class that listens for incoming connections. When I get an incoming connection, I want to signal an event to happen and pass that connected socket to a different thread...
1
by: Anthony Levensalor | last post by:
Hey guys! I'm writing a widget wherein I want the pages using it to be able to subscribe to events on it. I did my own research, not asking anyone to write it for me, just want to make sure I'm...
4
by: xend | last post by:
Hi. I want to add an event handler inside a function that will be used has an object constructor. The event handler to be added is an "method" of the object. if i do this: function...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.