.toArray()S2C Home « DOM Element Methods « .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

A JavaScript Array object.

.toArray() ExamplesTop

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, when the 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: