Becky Becky - 2 years ago 93
Javascript Question

create array in reverse order

I've got buttons with a common class (

). How can I add their
s in to an array in a reverse order?

var yourArray = [];
$('.buttons').each(function() {
yourArray.push( $(this).prop('id') );

Answer Source

You could create the array by adding each element to the beginning of the array using unshift():

var yourArray = [];
$('.buttons').each(function() {

Alternatively you can create it in the current order and then reverse() it. Also note that you can use map() to create the array initially:

var yourArray = $('.buttons').map(function() {

Finally you can use instead of creating a jQuery object just to access a property already accessible without the need of object creation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download