Search code examples
facebookfbjs

How create a function method on FBJS that reference 'this' as HTML element?


have tried to use this my own function on FBJS:

function addEvent(event,fun,pars){
pars.event=event;
pars.listen=function(){ fun(this, pars);return false; };
this.addEventListener(event,pars.listen,false);
}

but dont work...if call:

obj=document.getElementById("id_element");
obj.addEvent(....);

Firebug return this error:am123456789_obj.addEvent is not a function

Any idea to resolve?


Solution

  • You didn't create the addEvent function as a property of the object, so you can't call obj.addEvent(). The object simply doesn't know about that function.

    With FBJS, your best bet is to simply pass the object in as a parameter.

    function addEvent(obj,event,fun,pars){
    pars.event=event;
    pars.listen=function(){ fun(obj, pars);return false; };
    obj.addEventListener(event,pars.listen,false);
    }
    
    obj=document.getElementById("id_element");
    addEvent(obj,...);