Having used jQuery on a number of projects I’ve picked up a few handy shortcuts that are definitely worth sharing. There are no secrets here, just basic jQuery techniques discovered with the aid of the jQuery documentation. If you have any which you think are worth mentioning then please feel free to leave a comment.
Checking if the DOM is ready
Running code when the DOM is loaded and ready for manipulation is a popular requirement. The ideal solution is to place all initiating code at the bottom of your document (as per Yahoo’s "Best Practices for Speeding Up Your Web Site") but sometimes this is not possible. The next best alternative is to use a solid ‘DOM ready’ function such as the one jQuery offers. You normally access it like this:
$(document).ready(function(){...});
There’s a quicker way of accessing the same functionality:
$(function(){...});
Creating elements
There are a few different ways of creating elements, this is my favorite:
$('<div />').appendTo('body');
Alternatively you could take this route:
$('body').append('<div />');
Notice that you don’t have to write out the entire element. Even though DIV elements cannot have self-closing tags it’s still fine to specify it like above – JQuery will know what you mean.