Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through each child in a div with data-attribute

I have multiple <div>s, based on a <select>, where each one contains multiple <input>s and sometimes a disabled <select> with a fixed value. Now I'm trying to loop through each of the divs and put all inputs and select values into an array and push that array into the "master" array.

However this seems not to work to well.
I feel like I'm already close but yet so far. :(

var dummy = [];
    for(var i = 1; i <= toSend.count; i++){
        var temp = [];
        $("div[data-row="+i+"]").children('input, select').each(function(){
            temp.push( $(this).val() );
        });

        dummy.push(temp);
    };

    console.log(dummy); 

toSend.count is the counting of how many div's with data-row exist.
The HTML looks like this :

<div id="container">
   <div data-row="1">
      <input type="text"/>
      <input type="text"/>
   </div>
   <div data-row="2">
     <input type="text"/>
     <input type="text"/>
  </div>
</div>  

Aaah, nevermind this was my own stupidity! I'm generating the div's via AJAX
and I copy pasted myself an error.
All div's had data-row=1, no wonder it packed all in one array >.<

like image 595
Krenor Avatar asked Oct 19 '25 01:10

Krenor


1 Answers

(Edit: pays to read the code more completely)

Since the toSend variable is just the DIVs with a data-row attribute, no need to loop over toSend to find the DIVs:

var dummy = [];

$("#container div[data-row]").each(function() {
    var temp = [];

    $(this).children("input, select").each(function() {
        temp.push(this.value);
    });

    dummy.push(temp);
});

After this, you might not even need the toSend variable at all.

like image 191
Greg Burghardt Avatar answered Oct 21 '25 23:10

Greg Burghardt