Working with JSONP

The advent of JSONP — essentially a consensual cross-site scripting hack — has opened the door to powerful mashups of content. Many prominent sites provide JSONP services, allowing you access to their content via a predefined API. A particularly great source of JSONP-formatted data is the Yahoo! Query Language, which we'll use in the following … Continue reading

Events

jQuery provides simple methods for attaching event handlers to selections. When an event occurs, the provided function is executed. Inside the function, this refers to the DOM element that initiated the event. For details on jQuery events, visit the Events documentation on api.jquery.com. The event handling function can receive an event object. This object can … Continue reading

Inside the Event Handling Function

Every event handling function receives an event object, which contains many properties and methods. The event object is most commonly used to prevent the default action of the event via the .preventDefault() method. However, the event object contains a number of other useful properties and methods, including: link pageX, pageYThe mouse position at the time … Continue reading

How jQuery Works

link jQuery: The BasicsThis is a basic tutorial, designed to help you get started using jQuery. If you don't have a test page setup yet, start by creating the following HTML page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!doctype html><html><head> <meta charset="utf-8"> <title>Demo</title></head><body> <a href="http://jquery.com/">jQuery</a> … Continue reading

Plugins

A jQuery plugin is simply a new method that we use to extend jQuery's prototype object. By extending the prototype object you enable all jQuery objects to inherit any methods that you add. As established, whenever you call jQuery() you're creating a new jQuery object, with all of jQuery's methods inherited. The idea of a … Continue reading

How do I pull a native DOM element from a jQuery object?

A jQuery object is an array-like wrapper around one or more DOM elements. To get a reference to the actual DOM elements (instead of the jQuery object), you have two options. The first (and fastest) method is to use array notation: 1 $( "#foo" )[ 0 ]; // Equivalent to document.getElementById( "foo" ) The second … Continue reading

Don’t Act on Absent Elements

jQuery won't tell you if you're trying to run a whole lot of code on an empty selection – it will proceed as though nothing's wrong. It's up to you to verify that your selection contains some elements. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 … Continue reading

jQuery Learning Site

Learning Center Users There's a lot more to learn about building web sites and applications with jQuery than can fit in API documentation. If you're looking for explanations of the basics, workarounds for common problems, best practices, and how-tos, you're in the right place! Authors Too much good information is spread across corners of the … Continue reading

Event Helpers

jQuery offers a few event-related helper functions that save you a few keystrokes. Here is an example of one, the .hover() function. link .hover()The .hover() method lets you pass one or two functions to be run when the mouseenter and mouseleave events occur on an element. If you pass one function, it will be run … Continue reading

How do I get the text value of a selected option?

Select elements typically have two values that you want to access. First there's the value to be sent to the server, which is easy: 1 2 $( "#myselect" ).val();// => 1 The second is the text value of the select. For example, using the following select box: 1 2 3 4 5 6 7 <select … Continue reading

How do I select elements when I already have a DOM element?

If you have a variable containing a DOM element, and want to select elements related to that DOM element, simply wrap it in a jQuery object. 1 2 3 var myDomElement = document.getElementById( "foo" ); // A plain DOM element. $( myDomElement ).find( "a" ); // Finds all anchors inside the DOM element. Many people … Continue reading

Effects

jQuery makes it trivial to add simple effects to your page. Effects can use the built-in settings, or provide a customized duration. You can also create custom animations of arbitrary CSS properties. For complete details on jQuery effects, visit the Effects documentation on api.jquery.com.

Attributes

An element's attributes can contain useful information for your application, so it's important to be able to get and set them. link The .attr() methodThe .attr() method acts as both a getter and a setter. As a setter, .attr() can accept either a key and a value, or an object containing one or more key/value … Continue reading

Ajax Events

Often, you'll want to perform an operation whenever an Ajax request starts or stops, such as showing or hiding a loading indicator. Rather than defining this behavior inside every Ajax request, you can bind Ajax events to elements just like you'd bind other events. For a complete list of Ajax events, visit Ajax Events documentation … Continue reading

jQuery UI

jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. Whether you're building highly interactive web applications or you just need to add a date picker to a form control, jQuery UI is the perfect choice. jQuery UI contains many widgets that maintain … Continue reading

Beware Anonymous Functions

Anonymous functions bound everywhere are a pain. They're difficult to debug, maintain, test, or reuse. Instead, use an object literal to organize and name your handlers and callbacks. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 … Continue reading

Cache Length During Loops

In a for loop, don't access the length property of an array every time; cache it beforehand. 1 2 3 4 5 6 7 var myLength = myArray.length; for ( var i = 0; i < myLength; i++ ) { // do stuff }

Widget Factory

The jQuery UI Widget Factory is an extensible base on which all of jQuery UI's widgets are built. Using the widget factory to build a plugin provides conveniences for state management, as well as conventions for common tasks like exposing plugin methods and changing options after instantiation.

How do I determine the state of a toggled element?

You can determine whether an element is collapsed or not by using the :visible and :hidden selectors. 1 2 3 var isVisible = $( "#myDiv" ).is( ":visible" ); var isHidden = $( "#myDiv" ).is( ":hidden" ); If you're simply acting on an element based on its visibility, just include :visible or :hidden in the selector … Continue reading