javascriptjquerydocument-readydomready

Need some explanation about jQuery DOMready


I'm newbie to jQuery (I came to JavaScript from PHP).

I grouped multiple jQ functions in seperate files. I have the following questions:

  1. is there any requirement that every jQ function must be inside domready?
  2. What if, 1 of my files already has code that placed inside $(document).ready(function () { });. Can I use Domready more than 1 time?

Solution

  • No, you do not have to have every jquery function within the document ready handler. But if your code references any part of the DOM, you should have it within that context.

    You can have any number of functions bound to any event, including document.ready. So feel free to use $(document).ready(function () { }); or even better $(function () {}) all you want.