The abolished example of the form where the script adds new fields and therefore they are all of the same class.

<div class="forma"> <div class="initials"> <input type="text" name="first"> <input type="text" name="last"> </div> <div class="initials"> <input type="text" name="first"> <input type="text" name="lastn"> </div> <div class="initials"> <input type="text" name="first"> <input type="text" name="last"> </div> </div> 

How to extract this data for the subsequent $ ajax.post as an array

  {forma:[{firts: #, last: #}, {first: #, last:#}]} 

you first need to create an array, then find all the elements with the class .initials , create an object in it with the properties first & last and this object push into the array? And I get an array, but the elements in it are undefined

 var forma = new Array(); var len = $('.initials').size() ; for (var i = 0; i < len; i++) { var data = new Object(); data.first = $('.initials input[name="first"]:eq(i)').val(); data.last = $('.initials input[name="last"]:eq(i)').val(); ingred.push(data); }; console.log(forma); 

    1 answer 1

     var forma = []; $('.initials').each(function(index){ forma.push({ first: $('input[name="first"]', this).val(), last: $('input[name="last"]', this).val() }); }); // $(селектор, контекст); 
    1. Without context, each time the result will be for the whole document.
    2. It is recommended to use literal notation.
    • Thanks for the answer and advice - khex