1

How can I call a jQuery function from JavaScript?

//jQuery
$(function() {

    function my_func(){
           /.. some operations ../
    } 
});

//JavaScript
function js_func () {

   my_func(); //== call JQuery function 
}
2

6 Answers 6

1

Extend your function to jquery, then use it with $

    $.extend({
        my_jsFunc: function() {
            console.log('============ Hello ============');
        }
    });

    $(function(){
        $.my_jsFunc();
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Not sure y do you need this anyway here's a simple example...

$(function(){
    $('#my_button').click(function(){
       alert("buttonClicked");   //Jquery    
    });
});    

function my_func(){
    $('#my_button').click();  //JavaScript    
}

//HTML

<button id="my_button" onclick="my_func();"></button>

Comments

0

you are asking the wrong question but I think what you are searching for is:

$.my_func();

Comments

0

the my_func() is just normal function so should be kept outside jquery Dom ready function

$(function() {
    my_func();

});

 // This function should be outside from jquery function
  function my_func(){
       /.. some operations ../
   } 

Comments

0
$(function() {
    function my_func(){
       /.. some operations ../
    }
})

runs the everything inside when the page is "ready".

You don't need that.

Just define the function list this

function my_func(){
   /.. some operations ../
}

P.S. "How can i call a jQuery function from JavaScript?" is a bad question. jQuery is a library written using Javascript.

Comments

0

You can do it by actually changing the declaration to outside jQuery callback, but if you have some specific purpose following will work for you

$(function() {

    window.my_func = function() {
           /.. some operations ../
    } 
});

//JavaScript
function js_func () {

   my_func(); //== call JQuery function 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.