How to Create a Public Function in jQuery?

Estimated read time 1 min read

In jQuery, you can create a public function by attaching it to the $ object. Here’s an example:

$.myFunction = function(){
    console.log("This is my function.");
};

In this example, we are attaching a function called myFunction to the $ object, which is the main jQuery object. This function simply logs a message to the console.

Once you have defined your function, you can call it from anywhere in your jQuery code using the $.myFunction() syntax. For example:

$(document).ready(function(){
    $.myFunction();
});

In this example, we are calling the myFunction function when the document is ready.

By attaching your function to the $ object, you make it globally available to all of your jQuery code. This can be useful for creating utility functions or other functions that you want to be able to call from multiple parts of your code.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply