button onclick="e.preventDefault(); return true;" gives e is not defined in browser console

Viewed 1543

This is my button element:

<button id="loginButton" 
    ...
    onclick="e.preventDefault(); return true;"/>

But this approach gives me after login button click error in console:

Uncaught ReferenceError: e is not defined

Do you know how to fix my button to avoid "e is not defined" error in console?

2 Answers

e is a variable commonly used in event handlers. However you never defined e in your code. I recommend using an event listener in JS.

    document.getElementById('loginButton').addEventListener('click', function(e) {
      e.preventDefault();
      return true;
    });

This will accomplish what you need.

in events (like onclick="...") you can specify either a function name, then this function has to have a single parameter, where the event object will be passed. Or you can specify an JavaScript expression (as in your case), in this case there will be created implicit function (event) { ... } with your code placed inside.

I.e. in that case you have to use event (instead of e) as the name of the event object.

Related