The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> addEventListener in custom object
raphael75
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
raphael75
post 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!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
raphael75
post Apr 7 2012, 11:44 PM
Post #3


Novice
**

Group: Members
Posts: 28
Joined: 12-November 07
Member No.: 4,295



Any ideas?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Frederiek
post 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).
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th April 2024 - 04:39 PM