/    Sign up×
Community /Pin to ProfileBookmark

addEventListener in custom object

I’m trying to understand how a custom object can call a function within itself. Say I have something like this:

[CODE]function functiontest()
{

this.prop1 = ”,
this.prop2 = ”,
this.options = {initiallevels:0},
this.prop3 = ‘/blah’,
this.prop4 = true,

this.function4 = function(x)
{
console.log(x)
},

this.init = function(options)
{
this.uid=options.uid
this.t=options.target

[B]gebid(this.t).addEventListener(‘click’,function(){this.uid.function4(this.t)},false)[/B]

}
}

myfun1=new functiontest
myfun1.init({uid:’myfun1′,target:’target1′})

myfun2=new functiontest
myfun2.init({uid:’myfun2′,target:’target2′})
[/CODE]

And the html:

[CODE]
<html>
<head></head>
<body>

<div id=”target1″>T1</div>
<br>
<div id=”target2″>T2</div>

</body>
</html>
[/CODE]

I don’t know what to put in the part in bold to set the click event listener and have it call function4 within the functiontest object. I’ve tried a bunch of different things and nothing worked. Please help, thanks!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@raphael75authorApr 05.2012 — I could do something like:

document.getElementById(this.t).addEventListener('click',function(evt){var tmp=new functiontest();tmp.function4(this.t)},false)
[/QUOTE]

and that works, but there's got to be a better way to do it. Any help is appreciated, thanks!
Copy linkTweet thisAlerts:
@raphael75authorApr 08.2012 — Any ideas?
Copy linkTweet thisAlerts:
@Troy_IIIApr 08.2012 — I'm sure I don't understand what you are trying to do at all

but in case that you are asking if it's possible to add event listeners to native JS objects -I'll have to say "not".

Only DOM elements can.

What is your primary goal?
Copy linkTweet thisAlerts:
@Jeff_MottApr 08.2012 — I think the problem is that the value of "this" will vary depending on the context of the function being executed. In the case of a listener, "this" will be the event's target, which in this case will be a DIV element.

What you'll need to do is, outside of the listener callback, save the value of "this" in another variable.

<i>
</i>[color=red]var that = this;[/color]

gebid(this.t).addEventListener('click', function () {
[color=red]that[/color].uid.function4([color=red]that[/color].t);
}, false);


Once you get past that hurdle, you're likely to run into another problem with this line:

that.uid.function4()

The method "function4" belongs to the that/this object, not to the uid property.
×

Success!

Help @raphael75 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...