Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery click handler and Oriented object programming

I have a method in a class and into this method i have a handler for a click event in a div element:

function MyClass(container)
{
   this.Container=container;
   this.PrepareHandlers = function()
    {

        $('#Div1').click(function() {
            alert(this.Container);
        });
    }; 
}

But since im into the handler, "this" is the clicked element. Is possible to access to a property of an object from a handler declared inside a method?

like image 335
Jose3d Avatar asked Dec 16 '25 19:12

Jose3d


1 Answers

function MyClass(container)
{
   var self = this;
   this.Container=container;
   this.PrepareHandlers = function()
    {

        $('#Div1').click(function() {
            alert(self.Container);
        });
    }; 
}
like image 150
I.devries Avatar answered Dec 19 '25 08:12

I.devries



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!