0

$.each(val, function(ind, va) {
  console.log(ind)
  console.log(va)
  if (value.deductiontype == "1") {
    var row = $('<div class="row" style="margin-top:6px;"><div class="col-md-12"><div class="col-md-2"><label class=" col-form-label">' + ind + '</label></div><div class="col-md-10"><input class="form-control payments" type="text" id="" placeholder="' + ind + '" value="' + va + '"/><div></div></div>');

    $("#deduction_end_date").closest(".row").after(row);
  } else if (value.deductiontype == "2") {
    var row = $('<div class="row" style="margin-top:6px;"><div class="col-md-12"><div class="form-group"><div class="col-md-2"><label class=" col-form-label">' + ind + '</label></div><div class="col-md-10"><input class="form-control payments" type="text" id="" placeholder="' + ind + '" value="' + va + '"/></div></div></div></div>');
    $("#deduction_end_date").closest(".row").after(row);
  } else {

  }

});

I have above code. what it does is it appends data on a table by iterating over the passed data. It is working ok except that the data in the table are from last to first.

I want to append the data from first to last

How can I use .after() in a way that it will append from first to last

5
  • .reverse() the array i.e. val.reverse() Commented Jan 20, 2017 at 9:03
  • how can i use this reverse()? Commented Jan 20, 2017 at 9:03
  • 1
    @Satpal got it can you add it as answer so we can close this OP? Commented Jan 20, 2017 at 9:05
  • @Martin $.each(val.reverse(), function(ind, va) { ... Commented Jan 20, 2017 at 9:05
  • 1
    @RoryMcCrossan got it i want to give saptal the credit hope you understand this is the first time i heard the reverse :) Commented Jan 20, 2017 at 9:06

1 Answer 1

1

I want to append the data from first to last, You can simply use .reverse() the array.

The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.

$.each(val.reverse(), function(ind, va) {

});
Sign up to request clarification or add additional context in comments.

1 Comment

give me 3 minutes to tick the check :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.