addEventListener in custom object |
addEventListener in custom object |
raphael75 |
Apr 5 2012, 09:37 AM
Post
#1
|
Novice Group: Members Posts: 28 Joined: 12-November 07 Member No.: 4,295 |
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 gebid(this.t).addEventListener('click',function(){this.uid.function4(this.t)},false) } } myfun1=new functiontest myfun1.init({uid:'myfun1',target:'target1'}) myfun2=new functiontest myfun2.init({uid:'myfun2',target:'target2'}) And the html: CODE <html> <head></head> <body> <div id="target1">T1</div> <br> <div id="target2">T2</div> </body> </html> I don't know what to put in the gebid(this.t).addEventListener('click',function(){this.uid.function4(this.t)},false) part 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! This post has been edited by raphael75: Apr 5 2012, 09:41 AM |
raphael75 |
Apr 5 2012, 12:04 PM
Post
#2
|
Novice Group: Members Posts: 28 Joined: 12-November 07 Member No.: 4,295 |
I could do something like:
CODE document.getElementById(this.t).addEventListener('click',function(evt){var tmp=new functiontest();tmp.function4(this.t)},false) and that works, but there's got to be a better way to do it. Any help is appreciated, thanks! |
raphael75 |
Apr 7 2012, 11:44 PM
Post
#3
|
Novice Group: Members Posts: 28 Joined: 12-November 07 Member No.: 4,295 |
Any ideas?
|
Frederiek |
Apr 8 2012, 04:05 AM
Post
#4
|
Programming Fanatic Group: Members Posts: 5,146 Joined: 23-August 06 From: Europe Member No.: 9 |
I suggest you go see some javascript tutorials at javascriptkit.com : http://www.javascriptkit.com/javatutors/ (at Tutorial Index, on the second half of the page).
|
Lo-Fi Version | Time is now: 4th May 2024 - 04:25 AM |