.toArray() JQ Home  <<  D.E.M.  <<  .toArray()

Return the jQuery object as a JavaScript Array object.

Description

The .toArray() method returns an array of all the DOM elements contained in the jQuery object.

You can use all The JavaScript Array object methods and properties available on the returned object.

Using the .toArray() method is the same as using the .get() method without the parameter, but the name is more self documenting.

Syntax

Signature Description
.toArray()Return an array of DOM elements.

Parameters

None.

Return

An Array object.

.toArray() ExamplesD.E.M.  <<  Top

Return an array of all the DOM elements contained in the jQuery object.

In the example below we create a JavaScript Array object from the 'table' element below when the left mouse button is clicked. We then use the JavaScript Array object reverse() method to show we can use array methods on our created array on right button click.

Table For Testing The .toArray() Signature
Table Row 1, Table Data 1 Table Row 1, Table Data 2
Table Row 2, Table Data 1 Table Row 2, Table Data 2
Table Row 3, Table Data 1 Table Row 3, Table Data 2
Table Row 4, Table Data 1 Table Row 4, Table Data 2


$(function(){
  $('#btn6').on('click', function() {
	alert($('.testtable *').toArray());
  });
  $('#btn7').on('click', function() {
	alert($('.testtable *').toArray()
	                       .reverse());
  });
});

Press the button below to action the above code:

              

Related Tutorials

jQuery Basic Tutorials - Lesson 5 - DOM Element Methods