There is a form submit button:

<input type="submit" onclick="ProfileEditor.saveWork(this)" value="Сохранить"> 

as well as its handler:

 saveWork: function(btn){ alert('dd'); return false; }, 

When clicking, the alert is displayed properly, but then the standard form is submitted. So return false is not affected. What can be done?

  • 2
    and one more time # doing onclick in markup is a very bad practice. especially when using jQuery. I will post until you understand. e.preventDefault () would do it like that. - zb '
  • one
    but okay, I'm kind today> onclick = " return ProfileEditor.saveWork (this)" - zb ' '
  • one
    @eicto, there are no comments anymore. If we are talking about cross-browser compatibility, then in attachEvent this is a window , currentTarget is also absent, unlike onclick. Global variables are not an argument, then you can say that every answer you have is an appeal to $ / jQuery, because they are global objects. Via onclick, it is convenient to pass options, with a declarative approach (this is more convenient than data- *, which lose typing). Here is another example <img src="/fail.png" onerror="this.src = '/placeholder.png';"/> . I honestly don’t understand why you took my answer so badly. - RubaXa
  • one
    @Deonis; Still, this is not a holivar, but I don’t insist on writing through onXXXX, just labeling “this is bad” doesn’t bring benefit to anyone. The same story with new Function / eval, “evil and a point”, although this is a very important and most importantly necessary tool, without which many things are impossible, without which a modern developer cannot do. Of course, if you are not able to do it with skillful hands, this may end badly, but using any higher level framework you can shoot yourself a knee. - RubaXa
  • one
    I have already repeated a hundred times why I don’t make a difference, because here he will listen to you and will continue to terrorize and torment his horror, are you not afraid that this will remind you of your next birth? (actually still in this, colleagues who will massively set interceptors in the markup are already going upstairs to your office, they will still use global and goto, eval, etc., and they will have the same arguments as you, because taught them themselves. - zb '

2 answers 2

 <form action="/#" method="post" onsubmit="alert('submit!');return false"> <input type="submit" value="отправить"> </form> 

    There is a possibility that there are other handlers, listeners attached to this event or element, that send data from the form. Therefore, I can offer this version of the code:

     // отменяет событие отправки данных с формы event.preventDefault(); // останавливает цепочку вызова событий для следующих слушателей event.stopImmediatePropagation(); // проверим, вызвано ли событие preventDefault if(!event.isDefaultPrevented()){ // предотвратим действие по умолчанию (событие отправки с формы) event.returnValue = false; } 

    Yes, you need to add event as a parameter to the method. A few links to get acquainted with the description of the called methods and properties: