jQueryThe Advanced Events module offers programmers a number of frequently-needed shortcuts, for adding events to a page.
Probably the most important function included in the event library - it provides support for the following snippet:
$(document).ready(function(){
// Your code here...
));
In a nutshell, this is a solid replacement for using window.onload, and attaching a function to that. By using the above code, your Function will be called the instant the DOM is ready to be manipulated - which is exactly what 99.99% of all Javascript code needs to run.
This is a custom method which provides an 'in' to a frequent task. Whenever the mouse cursor is moved over a matched element, the first function is fired. Whenever the mouse moves off of the element, the second function fires. On top of this, checks are in place to see if the mouse is still within the element itself (for example, an image inside of a div), and if it is, it will continue to 'hover', and not move out (a common error in Javascript code).
$("p").hover(function(){
this.$old = this.innerHTML;
$(this).html("Welcome!");
},function(){
$(this).html(this.$old);
});
Shortcuts for using the bind and unbind functions are provided, where you would normally type:
$("p").bind("click",Function);
you can now type:
$("p").click(Function);
This is included for all of the following events: blur, focus, contextmenu, load, resize, scroll, unload, click, dblclick, mousedown, mouseup, mouseenter, mouseleave, mousemove, mouseover, mouseout, change, reset, select, submit, keydown, keypress, keyup, abort, error.
This is a shortcut, similar to the previous one, but for detaching a Function from a matched element.
$("p").unclick(Function);
Also, you can leave the function empty and completely remove all of a particular event type. For example, the code below removes all click events from the element.
$("p").unclick();
This is another shortcut, following the same rules as 'onclick, etc.', but will only fire once. This is very useful for buttons, links, etc.
$("p").oneclick(function(){
alert("Only once!");
});