1

i have textarea with text that is sperated with line break i want to loop this list in reverse order? so if i have text that loop like this

1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,2
1,1,1,1,1,1,1,3
1,1,1,1,1,1,1,4
1,1,1,1,1,1,1,5

it will read from 1,1,1,1,1,1,1,5 ... to .... 1,1,1,1,1,1,1,1 i know i can loop the text area like this :

$.each($('textarea[name=source]').val().split('\n'), function(e){
    alert(this);
 });

how can i convert it so it read the text in reverse order

Joel
  • 4,503
  • 1
  • 27
  • 41
user63898
  • 29,839
  • 85
  • 272
  • 514

2 Answers2

2

Use .reverse() and loop.

var myArray = $('textarea[name=source]').val().split('\n');
myArray.reverse();

$.each(myArray, function(){
    alert(this);
});
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
1

you can use reverse() directly instead of using a temporary variable to hold your array data.

jsfiddle demo

$.each($('textarea[name=source]').val().split('\n').reverse(), function(e){
    alert(this);
 });
kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51