How to pass an event of a button click to a callback function

3313 views javascript
4

Fiddle: http://jsfiddle.net/cmw0s2rk/

function HandleTopNavClick($elem, pretext = "", replace = false, e) {
  debugger;
  e.preventDefault();
  var href = $elem.href;
}

$("ul.check-it li a").on("click", HandleTopNavClick($(this), "clickhere_", true, event));

Any idea why the event is not carrying over to the function?

answered question

You're only meant to add a reference to HandleTopNavClick as the callback, not the result of calling it.

1 Answer

7

You are synchronously calling handleTopNavClick, then passing its result into .on. Instead, you want to create a function and pass that into .on, which will later be called back with the event.

$("ul.check-it li a").on("click", function(event) {
  HandleTopNavClick($(this), "clickhere_", true, event)
});

posted this

Have an answer?

JD

Please login first before posting an answer.