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 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

$ vs $()

Until now, we've been dealing entirely with methods that are called on a jQuery object. For example: 1 $( "h1" ).remove(); Most jQuery methods are called on jQuery objects as shown above; these methods are said to be part of the $.fn namespace, or the "jQuery prototype," and are best thought of as jQuery object … Continue reading

Use Stylesheets for Changing CSS on Many Elements

If you're changing the CSS of more than 20 elements using .css(), consider adding a style tag to the page instead for a nearly 60% increase in speed. 1 2 3 4 5 6 // Fine for up to 20 elements, slow after that:$( "a.swedberg" ).css( "color", "#0769ad" ); // Much faster:$( "<style type=\"text/css\">a.swedberg { … Continue reading

About jQuery

Getting started with jQuery can be easy or challenging, depending on your experience with JavaScript, HTML, CSS, and programming concepts in general. In addition to these articles, you can read about the history of jQuery and the licensing terms that apply to jQuery projects. You can also make a donation to help the jQuery team … Continue reading

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

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

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

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 select an item using class or ID?

This code selects an element with an ID of "myDivId". Since IDs are unique, this expression always selects either zero or one elements depending upon whether or not an element with the specified ID exists. 1 $( "#myDivId" ); This code selects an element with a class of "myCssClass". Since any number of elements can … 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

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.

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 test whether an element exists?

Use the .length property of the jQuery collection returned by your selector: 1 2 3 4 5 if ( $( "#myDiv" ).length ) { $( "#myDiv" ).show(); } Note that it isn't always necessary to test whether an element exists. The following code will show the element if it exists, and do nothing (with no … 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 }

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 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

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