What is the .on() method in jQuery?

Question

What is the .on() method in jQuery?

Answer

The .on() method in jQuery attaches an event handler, which is the event listener plus the callback function, to the selected element(s).

2 Likes

Thanks for your explanations, but i have seen on many site like w3school they don’t use .on() method in their example they directly attach even listen to the element selector like this -

$(".btn2").click(function(){
$(“p”).slideToggle();
});

So which way is better?

1 Like

Yoy might want to read this SO thread:

1 Like