Dec 20 2011

Using jQuery’s .pushStack() for reusable DOM traversing methods

by Karl Swedberg

The .pushStack() method has been in jQuery since before version 1.0, but it hasn’t received a whole lot of attention outside of core developers and plugin authors. While its usefulness may not be immediately apparent, it can come in really handy in some situations, so I’d like to take a quick look at what it does, how it works, and how we can use it.

pushStack Basics

At its most basic level, the .pushStack() method accepts an array of DOM elements and “pushes” it onto a “stack” so that later calls to methods like .end() and .andSelf() behave correctly. (Side note: As of jQuery 1.4.2, you can pass in a jQuery object instead of an array, but that isn’t documented and jQuery itself always uses an array, so that’s what we’ll stick to here.)

Internally, jQuery uses .pushStack() to keep track of the previous jQuery collections as you chain traversing methods such as .parents() and .filter(). This lets us traverse through the DOM, do some stuff, “back up” to previous collections within the same chain using .end(), and then do something else. Here is a somewhat contrived example:

Read the rest of this entry »

Levels: Advanced, Intermediate | Categories: DOM Traversing | 3 comments

Sep 02 2011

Using jQuery’s Data APIs

by Dave Methvin

In the beginning (well, beginning with jQuery 1.2.3 in early 2008) there was the jQuery.data() API. It offers a way to associate JavaScript data — strings, numbers, or any object — with a DOM element. As long as you manipulate the DOM element with jQuery, the library ensures that when the DOM element goes away, the associated data goes away as well. This is especially important for older versions of IE that tend to leak memory when JavaScript data is mixed with DOM data.

Most jQuery code sets data values using the higher-level .data() API; for example, $("div").data("imaDiv", true) sets a boolean value on every div in the document. This API, in turn, calls down to jQuery.data() with each element to set the value. For completeness, there are also jQuery.removeData() and .removeData() to remove data elements, and jQuery.hasData() to determine if any data is currently set for an element.

Read the rest of this entry »

Levels: Beginner, Intermediate | Categories: DOM Modification | 8 comments

May 03 2011

jQuery.map() in 1.6

by Jordan Boesch

Among all of the great fixes and additions to jQuery 1.6, I’m happy to say that jQuery.map() now supports objects! The previous map only supported arrays. With other libraries already offering object support for map, it was a nice addition.

Read the rest of this entry »

Levels: Intermediate | Categories: Utilities | 7 comments

Feb 17 2011

Merging jQuery Deferreds and .animate()

by Dan Heberden

Editor’s Note: This article originally appeared on danheberden.com.

jQuery’s .animate() method, and the shorthand methods that use it, are fantastic tools to create animations. Creating animations that link together to achieve a particular effect, and do something specific at the end of the animation, can be a painful, messy task. Luckily, we have .queue() for mashing animations together.

But what happens when you want to bridge the gap between ajax requests and animating? When you want to queue a bunch of animations, get data from the server, and handle it all at once, without a crap-load of nested callbacks? That’s when jQuery.Deferred() puts on its cape, tightens its utility belt, and saves the day.

Read the rest of this entry »

Levels: Advanced | Categories: Ajax, Effects | 2 comments

Sep 10 2010

Introducing jQuery API Search

by Karl Swedberg

Half-baked tutorials and plugins have been stacking up for months in my virtual kitchen, waiting for me to fire up the oven, finish the cooking, and spread them out on the table. For some reason, though, I’ve become less and less sure about whether I’ve put all the right ingredients into the mix. It’s irritating, to be sure, but I’m tired of fretting about it. I’m going to consider this the first of what I hope to be many “taste tests” — experiments in various degrees of completion thrown against the wall to see what, if anything, sticks.

As some of you may know, the online jQuery documentation went through a major overhaul in January of this year, coinciding with the release of jQuery 1.4. Packt Publishing “open sourced” the jQuery 1.4 Reference Guide that Jonathan Chaffer and I had been writing, allowing us to put its entire contents (and more) on api.jquery.com. Some of you may also know that the raw XML content of the site is available as a single file, which has allowed other sites such as jqapi.com and idocs.brandonaaron.net to provide alternative views of that content. But what most of you probably do not know is that the jQuery API has been available for quite some time as a searchable API that returns the results in JSON format.

Read the rest of this entry »

Filed under: Announcement | 5 comments

Jul 07 2010

Great Ways to Learn jQuery

by Marc Grabanski

These jQuery resources will set you on the path towards mastering jQuery.

Written Articles

  • Getting Started with jQuery – this is the official jQuery getting started guide.
  • jQuery for JavaScript Programmers – Simon Willison (creator of django) gives you an introduction to jQuery for people who already understand JavaScript.
  • jQuery Crash Course – Nathan Smith gives a quick introduction to jQuery on Digital Web Magazine.
  • Introduction to jQuery – Rick Strahl, well-known for his work developing with Microsoft technologies, gives his introduction to jQuery with part two covering using jQuery with ASP.NET.

E-Books

  • jQuery Fundamentals – open-source e-book written by Rebecca Murphey in collaboration with other well-known members of the jQuery community.
  • jQuery Enlightenment – Cody Lindley’s e-book covers advanced topics on jQuery with links to working code examples in jsbin.

Read the rest of this entry »

23 comments

Older Posts

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.