Book Image

Learning jQuery, Third Edition

Book Image

Learning jQuery, Third Edition

Overview of this book

To build interesting, interactive sites, developers are turning to JavaScript libraries such as jQuery to automate common tasks and simplify complicated ones. Because many web developers have more experience with HTML and CSS than with JavaScript, the library's design lends itself to a quick start for designers with little programming experience. Experienced programmers will also be aided by its conceptual consistency.Learning jQuery Third Edition is revised and updated for version 1.6 of jQuery. You will learn the basics of jQuery for adding interactions and animations to your pages. Even if previous attempts at writing JavaScript have left you baffled, this book will guide you past the pitfalls associated with AJAX, events, effects, and advanced JavaScript language features.Starting with an introduction to jQuery, you will first be shown how to write a functioning jQuery program in just three lines of code. Learn how to add impact to your actions through a set of simple visual effects and to create, copy, reassemble, and embellish content using jQuery's DOM modification methods. The book will step you through many detailed, real-world examples, and even equip you to extend the jQuery library itself with your own plug-ins.
Table of Contents (24 chapters)
Learning jQuery Third Edition
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Event methods


In order to react to user behavior, we need to register our handlers using these event methods. Note that many DOM events only apply to certain element types; these subtleties are not covered here. Event methods are discussed in detail in Chapter 3.

Binding

Event Method

Description

.ready(handler)

Bind handler to be called when the DOM and CSS are fully loaded

.bind(type, [data], handler)

Bind handler to be called when the given type of event is sent to the element

.one(type, [data], handler)

Bind handler to be called when the given type of event is sent to the element; removes the binding when the handler is called

.unbind([type], [handler])

Remove the bindings on the element (for an event type, a particular handler, or all bindings)

.live(type, handler)

Bind handler to be called when the given type of event is sent to the element, using event delegation

.die(type, [handler])

Remove the bindings on the element previously bound with .live()

.delegate(selector, type, [data], handler)

Bind handler to be called when the given type of event is sent to a descendant element matching selector

.delegate(selector, handlers)

Bind a map of handlers to be called when the given types of events are sent to a descendant element matching selector

.undelegate(selector, type, [handler])

Remove the bindings on the element previously bound with .delegate()

Shorthand binding

Event Method

Description

.blur(handler)

Bind handler to be called when the element loses keyboard focus

.change(handler)

Bind handler to be called when the element's value changes

.click(handler)

Bind handler to be called when the element is clicked

.dblclick(handler)

Bind handler to be called when the element is double-clicked

.error(handler)

Bind handler to be called when the element receives an error event (browser-dependent)

.focus(handler)

Bind handler to be called when the element gains a keyboard focus

.focusin(handler)

Bind handler to be called when the element, or a descendant, gains a keyboard focus

.focusout(handler)

Bind handler to be called when the element, or a descendant, loses keyboard focus

.keydown(handler)

Bind handler to be called when a key is pressed and the element has keyboard focus

.keypress(handler)

Bind handler to be called when a keystroke occurs and the element has keyboard focus

.keyup(handler)

Bind handler to be called when a key is released and the element has keyboard focus

.load(handler)

Bind handler to be called when the element finishes loading

.mousedown(handler)

Bind handler to be called when the mouse button is pressed within the element

.mouseenter(handler)

Bind handler to be called when the mouse pointer enters the element; not affected by event bubbling

.mouseleave(handler)

Bind handler to be called when the mouse pointer leaves the element; not affected by event bubbling

.mousemove(handler)

Bind handler to be called when the mouse pointer moves within the element

.mouseout(handler)

Bind handler to be called when the mouse pointer leaves the element

.mouseover(handler)

Bind handler to be called when the mouse pointer enters the element

.mouseup(handler)

Bind handler to be called when the mouse button is released within the element

.resize(handler)

Bind handler to be called when the element is resized

.scroll(handler)

Bind handler to be called when the element's scroll position changes

.select(handler)

Bind handler to be called when text in the element is selected

.submit(handler)

Bind handler to be called when the form element is submitted

.unload(handler)

Bind handler to be called when the element is unloaded from memory

Special shorthands

Event Method

Description

.hover(enter, leave)

Bind enter to be called when the mouse enters the element, and leave to be called when the mouse leaves it

.toggle(handler1, handler2, ...)

Bind handler1 to be called when the mouse is clicked on the element, followed by handler2 and so on for subsequent clicks

Triggering

Event Method

Description

.trigger(type, [data])

Trigger handlers for the event on the element, and execute the default action for the event

.triggerHandler(type, [data])

Trigger handlers for the event on the element without executing any default actions

Shorthand triggering

Event Method

Description

.blur()

Trigger the blur event

.change()

Trigger the change event

.click()

Trigger the click event

.dblclick()

Trigger the dblclick event

.error()

Trigger the error event

.focus()

Trigger the focus event

.keydown()

Trigger the keydown event.

.keypress()

Trigger the keypress event

.keyup()

Trigger the keyup event

.select()

Trigger the select event

.submit()

Trigger the submit event

Utility

Event Method

Description

$.proxy(fn, context)

Create a new function that executes with the given context