简体   繁体   中英

e.preventDefault - e is not defined

So I have this JS and when i run the code i get "e is not defined" - What am I missing?

$('ul.result-filters > li a').click(toggleFilters());

function toggleFilters(e) {
   e.preventDefault();
   //do more stuff
}

Use this :

$('ul.result-filters > li a').click(toggleFilters);

You were passing the result of toggleFilters() instead of passing the function itself.

$('ul.result-filters > li a').on('click', toggleFilters);

function toggleFilters(e) {
   e.preventDefault();
   //do more stuff
}

When adding the parenthesis after the function it's executed immediately and the results are passed back. To just reference the function and call it on click, drop the parenthesis.

This would also make the event available to the toggleFilters function!

You are calling function instead of passing handler name to click. Remove the parenthesis from toggleFilters()

Change

$('ul.result-filters > li a').click(toggleFilters());

To

$('ul.result-filters > li a').click(toggleFilters);

Can always do e = e || window.event e = e || window.event to ensure you have the event.

You need to use the function name instead of function call toggleFilters().

$('ul.result-filters > li a').on('click', toggleFilters);

function toggleFilters(e) {
 e.preventDefault();
//do more stuff
}

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM