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 
}
Hussain
  • 4,689
  • 5
  • 42
  • 66
Hark
  • 72
  • 5

6 Answers6

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>
Rohan Kumar
  • 39,838
  • 11
  • 73
  • 103
Shashank
  • 832
  • 1
  • 6
  • 14
1

Extend your function to jquery, then use it with $

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

    $(function(){
        $.my_jsFunc();
    });
sunil gautam
  • 451
  • 4
  • 6
0

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

$.my_func();
Aladdin
  • 384
  • 1
  • 5
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 ../
   } 
rajesh kakawat
  • 10,698
  • 1
  • 20
  • 39
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.

Paul Draper
  • 71,663
  • 43
  • 186
  • 262
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 
}
Loken Makwana
  • 3,618
  • 1
  • 19
  • 14