简体   繁体   中英

How to call function defined using jquery in js?

Here is sample jquery code

$(document).ready(function(){


  $('#myId').change(function(){
      regularJSfunc('data1','data2');
    });

}

now i want to trigger function call of $('#myId').change , can i do it through java script ?

jQuery actually provides a way for you to do that:

$("#myId").trigger("change");

Or you could chain it only the code you already have to call it immediately:

$("#myId").on("change", function(){
  regularJSfunc( 'data1', 'data2' );
}).trigger("change");

Using trigger call this function immediately on load script. So better use triggerHandler :

$('#myId').bind('change', function(){
    regularJSfunc( 'data1', 'data2' );
});

$('#myId').triggerHandler('change'); //call function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM