Pradeep
Pradeep

Reputation: 6603

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 ?

Upvotes: 1

Views: 112

Answers (2)

Vitalik Zenin
Vitalik Zenin

Reputation: 49

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

Upvotes: 0

Sampson
Sampson

Reputation: 268324

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");

Upvotes: 6

Related Questions