jQuery: The Write Less, Do More JavaScript Library

Utilities/jQuery.makeArray

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Utilities

jQuery.makeArray( obj )

Turns an array-like object into a true array.
Array-like objects have a length property and its properties are numbered from 0 to length - 1. Typically it will be unnecessary to use this function if you are using jQuery which uses this function internally.
Arguments:
objObject
Array-like object to turn in to an actual Array.

Examples:
Turn a collection of HTMLElements into an Array of them.

    var arr = jQuery.makeArray(document.getElementsByTagName("div"));
    arr.reverse(); // use an Array method on list of dom elements
    $(arr).appendTo(document.body);

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="../jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    var arr = jQuery.makeArray(document.getElementsByTagName("div"));
    arr.reverse(); // use an Array method on list of dom elements
    $(arr).appendTo(document.body);

  });
  </script>
  <style>
  div { color:red; }
  </style>
</head>
<body>
  <div>First</div>
  <div>Second</div>  
  <div>Third</div>
  <div>Fourth</div>
</body>
</html>

NameType