0

I'd like to attach an event when a function is called, but it seems not working using addEventListener...

My code :

function add () {
  this.inputString.input.addEventListener('mouseover', toggle.call(this));
}

function toggle () {
  $(this.tooltip).toggle();
}

Issue : the toggle() function doesn't work. However, this.inputString.input and this.tooltip are not emtpy...

4
  • what is "this" here? Commented Jun 29, 2016 at 9:42
  • 1
    If you want to bind a certain this use bind instead of call Commented Jun 29, 2016 at 9:47
  • Why it works with bind instead of call ? Commented Jun 29, 2016 at 9:57
  • call would "call" (i.e. invoke) the function immediately, bind would return a "bound function", which could be used as a handler. Commented Jun 30, 2016 at 12:44

1 Answer 1

1

The apparent problem I see here is that you should attach a function to addEventListener's second argument, not the call itself:

this.inputString.input.addEventListener('mouseover', toggle);

Then you should consider reviewing the toggle() function itself.
What do you mean by this.tooltip ?

But this is out of scope of this question (about attaching events).

Sign up to request clarification or add additional context in comments.

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.