Pergunta

I have this little script:

var moolang = new Class({
    initialize: function(element) { 

        this.el = $(element);
        this.el.addEvent('click', this.popup);  

    },

    popup: function()
    {
        //this.id = the id of the element.
    }
});

And i want to know "this" in the popup function. but if i try something like alert(this.el.id) it says there is no this.el :/

is there a way to know which class adds the event?

Foi útil?

Solução

Change the attach event so the callee has the proper context. Otherwise the context of an event listener will be the target element.

// Change this line
this.el.addEvent('click', this.popup);
//To this
this.el.addEvent('click', this.popup.bind(this)); // popup->this == this

jsfiddle here See mootools documentation. Binding the context of a function.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top