最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Oct 26, 2020 at 12:56 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Oct 26, 2020 at 12:08 mtmxmtmx 9471 gold badge15 silver badges34 bronze badges 5
  • 2 try to use event instead of e – AterLux Commented Oct 26, 2020 at 12:10
  • with event instead of e - i cannot log to application, nothing happens after 'login' click – mtmx Commented Oct 26, 2020 at 12:25
  • why something should happens? The script only prevents default action and do nothing. So, when nothing happens - it works as intended. – AterLux Commented Oct 26, 2020 at 12:41
  • But i cannot login into application if i change e -> event – mtmx Commented Oct 26, 2020 at 12:43
  • 1 yes, because event.preventDefault(); prevents default action (submitting the form or whatever it is) of the event. I.e. it starts working. Your question is not about logging in. It is about Uncaught ReferenceError – AterLux Commented Oct 26, 2020 at 12:45
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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

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

This will acplish what you need.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论