0

I want to pass more than one functions on document ready.

Doing something like

$(document).ready(myFunction; anotherFunction;);

did not work.

If I add parentheses, it will actually execute the function.

It works if I do

$(document).ready(myFunction);
$(document).ready(anotherFunction);

but , is there a better syntax where I can pass all the functions at once?

Something like

$(document).ready(myFunction, anotherFunction);

Thanks

slevin
  • 4,166
  • 20
  • 69
  • 129
  • You can revise the question http://stackoverflow.com/questions/1327756/can-you-have-multiple-document-readyfunction-sections – mbadeveloper Oct 14 '16 at 21:50
  • there is no diff calling them within the document ready like $(document).ready(function(){ myfunction1(); myfunction2(); }); – user669789 Oct 14 '16 at 21:51

2 Answers2

8

document.ready takes a single function as its argument. There is therefore no way to specify all of your functions as a list of parameters. You have a couple of different options for writing it differently with the same behaviour.

  1. Create a new function to wrap your others

    function initialise() { myFunction(); anotherFunction(); }

    $(document).ready(initialise);

  2. Use jQuery's $(document).ready shortcut syntax

    $(myFunction); $(anotherFunction);

3a) Wrap them all in one function passed to document.ready

$(document).ready(function () { myFunction(); anotherFunction(); });

3b) Or equivalently using the shortcut syntax

$(function() { myFunction(); anotherFunction(); });
clinton3141
  • 4,751
  • 3
  • 33
  • 46
1

Unfortunately .ready is only supported on the document and only accepts one function as an argument to call when the document is ready.
You can however use it multiple times, but there's no need to, just use an anonymous function wrapper instead

$(document).ready(function() {
    myFunction();
    anotherFunction();
});
adeneo
  • 312,895
  • 29
  • 395
  • 388