0

Why I'm not able to do this:

$(".item").on('click', 'a.close', closeItem());

        function closeItem() {
            $(".item").fadeOut({
                duration: 400,
                }
            });
        }

and yet this works

    $(".item").on('click', 'a.close', function() {
            $(".item").fadeOut({
                duration: 400,
                }
            });
    });

why cant you replace generic handler with a declared one?

Alko
  • 1,421
  • 5
  • 25
  • 51

1 Answers1

1

You can do it like this:

$(".item").on('click', 'a.close', closeItem);

function closeItem() {
    $(".item").fadeOut({
        duration: 400,
        }
    });
}

You need to pass the function as a parameter not call it. In the way you have done it you call the function and pass the result (undefined in this case) to the handler instead of the actual function.

mdatsev
  • 3,054
  • 13
  • 28