Q:

How to define a function in jQuery

0

How to define a function in jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the syntax $.fn.myFunction=function(){}

The syntax for defining a function in jQuery is little bit different from the JavaScript. Let's take a look at the following example to understand how to define a function in jQuery.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Define a Function in jQuery</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $.fn.myFunction = function(){ 
        alert('You have successfully defined the function!'); 
    }
    $(".call-btn").click(function(){
        $.fn.myFunction();
    });
});
</script> 
</head>
<body>
    <button type="button" class="call-btn">Click Me</button>
</body>
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to define a function in JavaScript... >>
<< How to show and hide div elements based on dropdow...