I first obtain an element:
var i=document.getElementsByTagName("fieldset");
Then I create a new object:
var a=new Fieldset(i[0]);
The function prototype is this:
slideDown: function() {
alert("here");
}
The constructor function is this:
function Fieldset(fieldset){
this.fieldset=fieldset;
this.fieldset.addEventListener("click",this.slideDown(this.fieldset),false);
};
The problem is that the slideDown function is called immediately when the new object is created and the listener is not attached to the element.
I’ve done the code with and without the this in front of fieldset in the constructor for the listener to no avail. I just don’t know where I’m fumbling this.
Well, in this expression
slideDown()is called beforeaddEventListener()since arguments are evaluated before the function is called.You probably mean to pass a function object as second argument rather than passing a value returned by
slideDown()(which doesn’t return anything anyway). You can construct such a function object this way:We use variable
selfto store the value ofthis. Note thatthisis not available insidelisteneranymore.