Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A jQuery object is a wrapper around an array of DOM elements. So a "jQuery array" <em>is</em> a jQuery object. The object contains every element on the page that matches the selector. Most methods you call on a jQuery object implicitly loop through all the elements in the array and apply to all of them.</p> <blockquote> <p>or if it's just got a lot of tolerance built into each of its functions to allow either a single element or a collection of elements.</p> </blockquote> <p>This is sort of the case. Really each method works on one element, but jQuery calls it as many times as it has objects to work on. I believe it actually uses <code>.each()</code> internally to do this, so if you understand how <code>.each()</code> works then you understand how jQuery's implicit looping works.</p> <p>Some methods do not apply to every object in the collection, but the ones that don't say so in their documentation. The exceptions are methods that it wouldn't make sense to apply to every element. For example, if you call <code>$('.blah').innerHeight()</code> you'll get the height of the first element in the jQuery object returned by <code>$('.blah.')</code>.</p> <p>There are also methods that can both get and set attributes. When getting they return the value from the first element, and when setting they apply to all matched elements. For example, if you call <code>$('.blah').css('font-size')</code> you'll get the size from the <strong><em>first</em></strong> element in the collection, but if you call <code>$('.blah').css('width', 100)</code> you'll set the width style of <strong><em>every</em></strong> element in the jQuery collection.</p> <p>Note that you can actually treat a jQuery object like an array. Just use array notation to access an element.</p> <pre><code>$jQ_obj = $('.classy').find('.refined'); element = $jQ_obj[3]; </code></pre> <p>This would store the fourth element on the page that has the class <code>refined</code> and is a child of an element that has <code>classy</code>. The resulting object in <code>element</code> is a raw DOM object.</p> <p>Second point,</p> <p>Since the answer that mentioned this was deleted, make sure you understand how <code>.each()</code> works. You need to pass it the function you want to be applied to each matched element, just like the event handler. So it should be:</p> <pre><code>jQuery('.someclass').each(function(i) { //don't forget the dot! $(this).click(function() { alert("I've been clicked!"); }); }); </code></pre> <p>Inside the callback to <code>.each()</code> the <code>this</code> keyword refers to the current DOM element being looped through, and the arg passed to the callback is the loop index. <code>this</code> is a raw DOM element (think of it like writing <code>$('.someclass')[i]</code>) so you have to create a jQuery object from it to do call <code>.click()</code> on it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload