How to Call jQuery function from JavaScript with example

I have heard many times people saying that I need to call jQuery function from JavaScript. I told them there is no special technique to do that. Just put your fingers on your keyboard and start coding as you normally do. You will get to know that it’s very much easy. But now its really became a wanted question to me. Because so many people asked me this question during the last few months.

So here in this JavaScript tutorial, I will show you calling jQuery function from JavaScript with an example.

You may also interested in,

Add and remove class dynamically to an element using jQuery

Real Time Chat Application PHP With File Transfer System AJAX

Why use javascript:void(0) in hyperlinks?

Call jQuery function from JavaScript

the shortest way to declare a jQuery function is:

$(function(){
// Your code goes here
});

Now assume that you have a JavaScript function named: my_js_function()

Now you have to call a jQuery function from this my_js_function()

In order to access a jQUery function from a JavaScript function, you can declare a variable and assign it to a jQuery function. Finally, just call it from a JavaScript function.

var my_jquery_function;
// this is jQuery function
$(function(){
    my_jquery_function = function( _text )
    {
        alert( _text );
    }
})
// This is javascript function
function my_js_function()
{
    //Invoke jQuery Function
    my_jquery_function("I am called from a JavaScript Function");
}

In this way you can call a jQuery function from JavaScript.

How to run multiple JavaScript functions onclick

Get user’s Latitude and Longitude in JavaScript – HTML5 Geolocation

 

2 responses to “How to Call jQuery function from JavaScript with example”

  1. vincent says:

    It’s a bit annoying when people give an explanation such as “Just put your fingers on your keyboard and start coding as you normally do”. In your example, you seem to be DEFINING a jquery function. I don’t want to do that — I want to call a jquery function that already exists — namely the well known jquery function “ajax”. But even though I’m coding as I normally would — I still get “ajax is not defined” in the console.

    • Saruque Ahamed Mollick says:

      Did you include jquery in your file? If yes then check again if you are calling the function after the line jquery has been included. jquery needs to be loaded first.

Leave a Reply