Javascript – Calling jQuery’s DOM ready function from another function

javascriptjquery

Is it possible to call jQuery's $(document).ready function from within another Javascript function? I have a button, which when clicked should call the jQuery DOM ready function. Is this possible?

Best Answer

If you are calling it from another javascript function, drop the $(document).ready part

function f1(){
 $('#idhere').something;
}

or if you want to call a function only when the document is ready, put the function call inside $(document).ready

$(document).ready(function(){
 function f1(){
  //do something here
 }
});

But you are not taking advantage of jquery's full functionality here. If you want to associate an onclick event with an element, you can do it like so:

<button id='button1'></button>

In the js file:

$('#button1').click(function(){
 //do the onclick sequence here
});