8

I have a problem

    open: function($type) {
          //Some code
          document.getElementById($type).addEventListener("click", l.close($type), false);
    },
    close: function($type) {
           //There is some code too
           document.getElementById($type).removeEventListener("click", l.close($type), false);
           //^ Recursion & Uncaught RangeError: Maximum call stack size exceeded
    }

What I'm doing wrong? Without this click event listener everything is working well. And what is the third parameter doing (true|false)? Thank you.

1
  • developer.mozilla.org/en/DOM/element.removeEventListener the third parameter flags whether or not you want the event listener to use event capturing (as oppose to bubbling) on adding, and on removal whether or not the event was added as such. Commented Feb 29, 2012 at 11:04

2 Answers 2

9

You are calling the close function in the addEventListener and removeEventListener when you are trying to pass is as an argument (causing an infinite loop). Instead you should simply pass the reference to the function as follows:

document.getElementById($type).addEventListener("click", l.close, false);

And:

document.getElementById($type).removeEventListener("click", l.close, false);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. :) But what is the third parameter doing?
@Krylovech It defines if the handler should be executed during capturing or bubbling phase: quirksmode.org/js/events_order.html
0

Or you might have two Javascript functions with the same name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.