469,631 Members | 1,734 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

.addEventListener() not finding the right object method

Hello all,

I have a custom class object with some prototype methods like
setKeyDownEvent, etc.

The problem is on my webpage, after I instantiate the class, I try to
do .addEventLister() to a textarea by attaching my custom class
object's setKeyDownEvent,etc to it. Long story short, here is an
example of my code.

----------------

var myownobject ;

window.onload = init;

function init()
{
myobj= = new MyObject();
initListeners();
}

function initListener()
{
if (navigator.userAgent.indexOf("Safari") > 0) {

myobj.getTextarea().addEventListener("keydown",myo bj.setKeyDownEvent,true);

myobj.getTextarea().addEventListener("keyup",myobj .setKeyUpEvent,true);
} else if (navigator.product == "Gecko") {

myobj.getTextarea().addEventListener("keypress",my obj.setKeyDownEvent,false);

myobj.getTextarea().addEventListener("keyup",webcm d.setKeyUpEvent,false);

} else {

myobj.getTextarea().attachEvent('onkeydown',myobj. setKeyDownEvent,false);

myobj.getTextarea().attachEvent('onkeyup',myobj.se tKeyUpEvent,false);
}
}

MyObject is a custom class I have and the problem is inside its
MyObject.setKeyDown mehtod, I'm calling other methods of MyObject by
doing like

Myobject.prototype =
{
setKeyDownEvent : function()
{
this.method2()
}
}
....

so when a say key event is pressed, firefox say this.mehtod2() is not a
function (i know this module works btw, because before I did the
traditional way like so, it worked)

<script ...>
...
function kd(e)
{
myobj.setKeyUpEvent(e)
}
function ku()
{
myobj.setKeyUpEvent(e)
}
</script>
<textarea id="mytextarea" oonKeyPress="kd(event);" onKeyUp="ku(event)"
/>


so I'm thinking, this's try to find "this" inside window object instead
of MyObject.

Aug 26 '05 #1
4 2123
and oh btw? this.method2() is just an example here, it's there in the
function, trust me.. like

Myobject.prototype =
{

mehtod2:function{
},
....
}

thanks again.

Aug 26 '05 #2
On 26/08/2005 15:37, Liming wrote:
I have a custom class object with some prototype methods like
setKeyDownEvent, etc.

The problem is on my webpage, after I instantiate the class, I try to
do .addEventLister() to a textarea by attaching my custom class
object's setKeyDownEvent,etc to it.
I answered a question on this same topic yesterday. See
<URL:http://groups.google.co.uk/group/comp.lang.javascript/browse_thread/thread/9b041b9103b6de0f/2065870c97d74b50?tvc=2>.

[snip]
if (navigator.userAgent.indexOf("Safari") > 0) {


Don't use browser detection. See
<URL:http://www.jibbering.com/faq/#FAQ4_26> and the relevant links.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Aug 26 '05 #3
Mike,

That's a GREAT tutorial, wow, that was good. But I do have this
question...

You have the var instance = this; defined in the class (private
variable).. and such it's accessible to the inner functions of the
"constructor" class ONLY.

what if you are calling from a method of .prototype.mehtod()? In this
case, i wont' have access to the private instance variable "instance".
What's the best way to do this? Move this prototype method into the
class "consturctor" as an inner function?

I just recently picked up javascript, have a lot stupid questions,
please bear with me.

Aug 26 '05 #4
On 26/08/2005 19:58, Liming wrote:

[snip]
You have the var instance = this; defined in the class (private
variable).. and such it's accessible to the inner functions of the
"constructor" class ONLY.

what if you are calling from a method of .prototype.mehtod()? In this
case, i wont' have access to the private instance variable "instance".
What's the best way to do this? Move this prototype method into the
class "consturctor" as an inner function?


[snip]

You could do that, though it's not the only solution. An alternative is
to create a property on the element that references your object. When
your listeners are executed, the this operator will reference the element:

function MyObject() {
/* ... */
}
MyObject.prototype.listener = function() {
/* Use 'this.object' to get reference to object instance. */
};

var o = new MyObject();

myElement.object = o;
myElement.addEventListener('...', o.listener, false);

Hope that helps,
Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Aug 26 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Joe Kelsey | last post: by
4 posts views Thread by PJ | last post: by
1 post views Thread by Andrew Poulos | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.