$("p").click(function(){
// action goes here!!
});
In the above jquery code why we pass a function() to the event?
$("p").click(function(){
// action goes here!!
});
In the above jquery code why we pass a function() to the event?
Because the click method is defined this way (the method parameter ).
See:
click
You have to understand its not a default JavaScript method, this method is defined in the JQuery
so you have to call it as it is defined in the method.
Behind the scene actually when JQuery
registering any event with DOM its only says to do one thing which is calling your provide method.
The simplest answer: according to documentation.
https://api.jquery.com/click/
When a user clicks a p
element, jQuery
is supposed to fire an event and execute something. How is it supposed to pass actions, if not using functions?
It's called a callback or an anonymous function that you want executed when the event happens. You'll notice it's not just jQuery, but almost all Javascript frameworks will expect one. It's usually whenever you are binding to events, executing functions that might take a while to return, or functions that execute other functions.
But you can also provide it a function name if you want.
function clickedMe(){
alert("Something clicked me");
}
$("p").click(clickedMe)