0

I have a click event as follow which works fine:

$('#showmenu').off('click').on('click', function(e) {
    e.preventDefault();
    e.stopPropagation();
    show_menu();
    return false;
});

where show_menu is:

function show_menu(e) {
    if (!collapseForm.is(':visible')) {
        collapseForm.show();
        showMenu.removeClass(chevronDown).addClass(checvronUp);
        searchAgain.hide();
    } else {
        collapseForm.hide();
        showMenu.removeClass(checvronUp).addClass(checvronDown);
        searchAgain.show();
    }
}

I would like to be able to do something like that:

$('#showmenu').off('click').on('click', show_menu(e));

Is it possible to pass "e" to the callback function by doing the following?

function show_menu(e) {
    e.preventDefault();
    e.stopPropagation();
    if (!collapseForm.is(':visible')) {
        collapseForm.show();
        showMenu.removeClass(chevronDown).addClass(checvronUp);
        searchAgain.hide();
    } else {
        collapseForm.hide();
        showMenu.removeClass(checvronUp).addClass(chevronDown);
        searchAgain.show();
    }
    return false;
}

enter image description here

1 Answer 1

3

The event object is passed to the function when the function is called (by the event firing).

You have to pass the function to the on method.

$('#showmenu').off('click').on('click', show_menu);
Sign up to request clarification or add additional context in comments.

4 Comments

I am getting: Uncaught ReferenceError: e is not defined, see updated question
@Alex — I'm not getting that: jsbin.com/subecom/1/edit?html,js,console,output
see screenshot attached
@Alex — In the question you wrote function show_menu(e) { e.preventDefault();. That isn't what the screenshot shows.

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.