Sunday, July 31, 2011

get() → Array

This serves as a backwards-compatible way of accessing all matched elements (other than the jQuery object itself, which is, in fact, an array of elements). It is useful if you need to operate on the DOM elements themselves instead of using built-in jQuery functions.
Example:

Selects all divs in the document and returns the DOM Elements as an Array, then uses the built-in reverse-method to reverse that array.
function disp(divs) {
      var a = [];
      for (var i = 0; i < divs.length; i++) {
        a.push(divs[i].innerHTML);
      }
      $("span").text(a.join(" "));
    }
    
    disp( $("div").get().reverse() );
Reversed - <span></span>

  <div>One</div>
  <div>Two</div>
  <div>Three</div>
span { color:red; }

No comments:

Post a Comment