Iterate over a jQuery collection without having to rebuild the jQuery object

$(".item").each (i, elt) -> $(elt).attr("href") # ...

Doing $(elt) is necessery to get the jquery object. Is there a way to iterate over a jquery collection, without having to rebuild the jquery object?


Doing $(elt) is necessery to get the jquery object.


Is there a way to iterate over a jquery collection, without having to rebuild the jquery object?

No. Because the jQuery object you have is a set of all of the matched elements. To use the jQuery methods on individual elements, you need to get a jQuery object containing just the individual element you want to act on. In a loop, that means wrapping your elt (or this, as both are the same DOM element).

For lots of things, though, you don't need jQuery methods. Your example is one of those things, just use elt.href directly:

$(".item").each (i, elt) -> var someVar = elt.href; # ...

The DOM2 HTML spec and the newer HTML5 spec both list lots of reflected properties you can use directly. id, for instance, is one in particular people frequently use. You see $(this).attr("id") a lot where this.id would be sufficient (and more efficient, although it's extremely rare that the efficiency matters).


Yes, cache the jQuery object then select by index.

var $items = $(".item"); $items.each(function(i) { $items.eq(i).doStuff(); });


  • What is wrong with this Macro?
  • How to import plain javascript helper classes into React?
  • Assigning a swift closure (block equivalent) to an existing objective-c block being accessed using a
  • Grouping string variables into factors in R
  • Dynamically Impersonate a remote user - c# and asp.net
  • overlapping appointments using the entity framework
  • CSS - how to trim text output?
  • Is an if-let or a normal if condition better?
  • Bootstrap collapse within a foreach loop
  • mapping between two ontologies
  • Is there a way to link a linux's thread TID and a pthread_t “thread ID”
  • matching similar elements in between two lists
  • Cannot invoke my method on the array type int[]
  • Lua: Line breaks in strings
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • Iron Router: How do I send data to the layout?
  • CSS bleed-through with cfinput type=“datefield”
  • Possible to get mouse events fired when cursor is outside page?
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • When should I choose bucket sort over other sorting algorithms?
  • Weird JavaScript statement, what does it mean?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Unanticipated behavior
  • Transpose CSV data with awk (pivot transformation)
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • C# - Getting references of reference
  • using HTMLImports.whenReady not working in chrome
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Reading document lines to the user (python)