Search code examples
javascriptjqueryarraysobject

how to insert data inside an array from each loop in jQuery


From last 5 hours i am trying to solve out one single thing and think almost made it but stuck somewhere due to which not getting the result i want. I think here i need a bit expert level help to rectify where i am doing wrong.

var eduarray = [];
$('.education-groupbox').each(function(index, el) {
    eduarray[index] = [];   
    var s = $(this).attr('id');
    //console.log();
    $('#'+s+' .inputs').each(function(key, value) {

        //eduarray['index'].push("rohit");

    });

});

What i am trying to get is the result in format of object with multiple array from the each each loop so that i can send the data through formdata and process the php form.

 <div id="education-groupboxwrapper">
                    <div id="education-groupbox" class="education-groupbox">
                      <div class="inputs col-3">
                        <label for="email">Bölüm</label>
                        <input type="text" name="email" />
                      </div>
                      <div class="inputs col-3">
                        <label for="email">Okul Adı</label>
                        <input type="text" name="email" />
                      </div>
                      <div class="inputs col-3">
                        <label for="email">Bölüm</label>
                        <input type="text" name="email" />
                      </div>
                      <div class="inputs col-3">
                        <label for="email">Bölüm</label>
                        <input type="text" name="email" />
                      </div>
                    </div> 
                  </div>

Solution

  • From your code you I found some bug, $('#'+s+' inputs').each won't work since ID are unique, use class instead so you can have multiple inputs under same input group with the same class name.

    Use eduarray[index].push($(this).val()); to push value into array.

    $('.education-groupbox').each will loop though each block of HTML for <div class="education-groupbox"> therefore inside the each using $(this).find('input') will return all input inside this block, then you can use each to push all input value to the array.

    var eduarray = [];
    $('.education-groupbox').each(function(index, el) {
      var _this = $(this);
      eduarray[index] = [];
      _this.find('input').each(function(key, v) {
        eduarray[index].push(v.value);
      });
    
    });
    
    console.log('eduarray[0] -->' + eduarray[0]);
    console.log('eduarray[1] -->' + eduarray[1]);
    console.log('eduarray    -->' + eduarray);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div id="education-groupboxwrapper">
      <!-- groupbox 1 -->
      <div class="education-groupbox">
        <div class="inputs col-3">
          <label for="email">Bölüm</label>
          <input type="text" name="email" value="test1" />
        </div>
        <div class="inputs col-3">
          <label for="email">Okul Adı</label>
          <input type="text" name="email" value="test2" />
        </div>
        <div class="inputs col-3">
          <label for="email">Bölüm</label>
          <input type="text" name="email" value="test3" />
        </div>
        <div class="inputs col-3">
          <label for="email">Bölüm</label>
          <input type="text" name="email" value="test4" />
        </div>
      </div>
      <!-- groupbox 2 -->
      <div class="education-groupbox">
        <div class="inputs col-3">
          <label for="email">Bölüm</label>
          <input type="text" name="email" value="test111" />
        </div>
        <div class="inputs col-3">
          <label for="email">Okul Adı</label>
          <input type="text" name="email" value="test222" />
        </div>
        <div class="inputs col-3">
          <label for="email">Bölüm</label>
          <input type="text" name="email" value="test333" />
        </div>
        <div class="inputs col-3">
          <label for="email">Bölüm</label>
          <input type="text" name="email" value="test444" />
        </div>
      </div>
    </div>