iterate values in $ .each nested Jquery

5

I'm getting a full table from my server side, I'm looking for td with information that I want and saved this entire tr in a variable like below:

var rowVoo;

$(table).find("tr td").each(function () {
      if ($.trim($(this).text()) == "Porto Velho - RO") {
           rowVoo = $(this).closest("tr");
      }
});

The variable rowVoo will look like this:

<tr>
    <td>Porto Velho - RO</td>
    <td>11</td>
    <td>1 ( 9.1 %)</td>
    <td>0 ( 0 %)</td>
    <td>0 ( 0 %)</td>
</tr>

I have a list as an abbreviation:

<ul class="list-voos">
      <li><span></span> VOOS PREVISTOS</li>
      <li><span></span> ATRASADOS AGORA</li>
      <li><span></span> VOOS CANCELADOS</li>
      <li><span></span> ATRASADOS NO DIA</li>
</ul>

I need to pass the values that are within the td to the span that are within the list.

I search for the span that are within the list:

var list = $(".list-voos").find("li").find("span");

And I try to iterate the values:

$(rowVoo).find("td").each(function () {
     var that = $(this);
     $(list).each(function () {
         console.log($(this).text(that.text()));
         return null;
     });
});

But the values are duplicated and the span of the list always gets the last value of td

    
asked by anonymous 03.07.2014 / 23:14

2 answers

5

You must use index as .each () / make available.

Test like this:

var lis = $('ul.list-voos li');
$(rowVoo).find("td").each(function (i) {
     $(lis).find('span').eq(i).html(this.innerHTML);
});
  

function (index, value) {

The each method allows a function that gives you two variables. The first is the index of the element in the collection, the second is the element itself of the collection. In the case of jQuery, it is the same as this within that function.

Example: link

    
03.07.2014 / 23:34
2

Highest Performance [Updated]:

var i, e, tableTeste = $("table").find("tr td"),
  ulLi = $(".list-voos").find("li span");

for (var i = 0, e = tableTeste.length; i < e; i++) {
  var tdValue = tableTeste[i].innerHTML;

  ulLi.eq(i).html(tdValue);


}

before:

var i, e, tableTeste = $("table").find("tr td"),
  ulLi = $(".list-voos").find("li span");

for (var i = 0, e = tableTeste.length; i < e; i++) {
  var tdValue = tableTeste.eq(i).html();
  ulLi.eq(i).html(tdValue);

}

see the comparison using each (), for () {} and for () {} with innerHTML: link

    
01.09.2014 / 23:14