How to define a function in jQuery

How to define a function in jQuery

In this post we will give you information about How to define a function in jQuery. Hear we will give you detail about How to define a function in jQueryAnd how to use it also give you demo for it if it is necessary.

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-1.12.4.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" >Click Me</button>
</body>
</html>

 

Hope this code and post will helped you for implement How to define a function in jQuery. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US