In my code I am binding event handlers to document:
$(document).bind("myEvent", function() { ... });
Do I have to wait for the ready event (i.e. use $(document).ready) to bind event handlers in general and when I bind them to document in particular?
$(document).readyis used to ensure the DOM is ready before trying to interact with it. If you put your script in the<head>then I suggest you put your code within this block if you interact with the DOM. If you put your script at the bottom of the<body>after the DOM elements then its not required – but I would still suggest its used.Update
Attaching an event handler to the
documentdoesn’t require the code to be within thereadyhandler but its often used for consistency, some people (me included) place all script to attach event handers (regardless of the target) in areadyhandler.You will notice that on the documentation for
bind()that elements must exist before the handler is attached.Also note that
.on()is the preferred and suggested method to use inplace of.bind()as of jQuery 1.7