Exit the click handler

How to exit the event?

$('.more').click(function() {
   if (condition1) {
      if (condition2) {
         // abort, exit completely out of click handler
      ...
+3
source share
4 answers

Use returnthere:

$('.more').click(function() {
   if (condition1) {
      if (condition2) {
         // abort, exit completely out of click handler
         return;

Cm:

Function interruption Safe

+11
source

return;

This will lead you to the function associated with the on-click handler;

+1
source

Please note that you can also use break here.

$('.more').click(function() {
   if (condition1) {
      if (condition2) {
         // abort, exit completely out of click handler
         break;
+1
source

I no longer use anonymous functions because my understanding of JavaScript jumped to a new plateau when I started naming them. And, in addition, naming your functions provides a kind of built-in documentation:

$('.more').click(myFunction)

function myFunction() {
   if (condition1) {
      if (condition2) {
         return
      }
   }
}
0
source

Source: https://habr.com/ru/post/1747439/


All Articles