3

If i'm writing event in this way, action executes when it should:

 document.getElementById('myElem').onmousedown = (e) => {
     console.log('fired!')
    }

But if i'm writing same stuff in other way, action executes when page is loaded, once:

let HandleEvent = (event) => {
 console.log('fired!')
}
document.getElementById('myElem').onmousedown = HandleEvent(event)

UPD: Ofcourse it is just example, HandleEvent function will have much more complex logic.

My questions is:

  1. Why?
  2. How to make it work properly?
1
  • 5
    onmousedown expects a function reference, but what you're passing is the returned value of the function (which is undefined) because you're invoking it. Try document.getElementById('myElem').onmousedown = HandleEvent instead. Commented Jul 7, 2016 at 11:12

6 Answers 6

6
document.getElementById('myElem').onmousedown = HandleEvent;

Would do what you want; you should not execute the handler when assigning it. You should just assign the function reference.

Then you can also assign it as the onload handler, or even call it yourself (if you are not relying on the event object)

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

Comments

5
document.getElementById('myElem').onmousedown = HandleEvent;

To pass additional arguments you could use bind:

document.getElementById('myElem').onmousedown = HandleEvent.bind(null, argument1, argument2);

...or you could use a factory function:

document.getElementById('myElem').onmousedown = createHandler();

function createHandler() {
  var a = calculateA();
  var b = calculateA();
  return function handleEvent() {
     //use a and/or b
  };
}

There are other ways too. Precise code will depend on your use case.

6 Comments

how can i pass additional params to function?
@Src Function.prototype.bind
@Src: That wasn't your question. Ben answered the question you asked. (And you can readily find an answer to that question by searching SO, where it's been very, very thoroughly answerd.)
Function.bind or wrap it in a another function that calls HandleEvent passing it the parameters you want
Works perfectly, thank you very much! I already accepted answer before you edited the question, so i think it would not be right to re accept it. sorry :\
|
2

You don't need the event.

let HandleEvent = (event) => 'fired!';
document.getElementById('myElem').onmousedown = HandleEvent

Comments

1

Remove the event

let HandleEvent = (event) => {
 console.log('fired!')
}
document.getElementById('myElem').onmousedown = HandleEvent

1 Comment

Remove the event is not a very good description of what removing the parentheses does.
1

If you want to pass an attribute

let HandleEvent = (event) => {
 console.log('fired!')
}
document.getElementById('myElem').onmousedown = HandleEvent.bind(attribute)

Comments

0

What you did was assigning the result of HandleEvent() to the event, instead you should assign the function to it.

Correct statement:

document.getElementById('myElem').onmousedown = HandleEvent;

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.