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
  • 5,057
  • 6
  • 45
  • 71
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
  • 40,431
  • 11
  • 76
  • 106
Shashank
  • 830
  • 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
  • 3
  • 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,826
  • 1
  • 21
  • 40
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
  • 78,542
  • 46
  • 206
  • 285
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,788
  • 2
  • 21
  • 14