How to define a function in jQuery?
In this tutorial, we are going to see how to define a function in jQuery? The following example will show you how to define a function in jQuery. Try to test the following code, you will see a button “Click here”, when you click on it, in the jQuery code (line 12) we call our function “myFunction()” that we defined in line 8.
How to define a function in jQuery?
<!DOCTYPE html> <html> <head> <title>How to define a function in jQuery?</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $.myFunction = function(){ alert('You have successfully defined the function!'); } $(".btn").click(function(){ $.myFunction(); }); }); </script> </head> <body> <button type="button" class="btn">Click here</button> </body> </html>
Result |
---|