juanpscotto
juanpscotto

Reputation: 1050

How to get the index of a tr when traversing the table and when adding a new element

I'm traversing a table with jquery, but I want to get the index of the tr at specific moments of the tranvers table:

$('table#tabla_materia tr').each(function()
{
   if(typeof ($(this).find('input.cep').val()) !== 'undefined'){


  // get the index here
  rowIndex
  }


// get the index of the new element
var nuevoRegistro = tbl_materia.insertRow(rowIndex+1);
nuevoRegistro.setAttribute('class', 'cep');

}   

Can you help me please.

Upvotes: 1

Views: 267

Answers (1)

dfsq
dfsq

Reputation: 193291

You can read either rowIndex property of the HTMLTableRowElement, or use index parameter passed to $.fn.each callback.

$('table#tabla_materia tr').each(function (index) {
    console.log(index, this.rowIndex);
    // ...
});

Well, to be exact, index in $.fn.each callback is actually the index of the table row element in corresponding jQuery collection. However if you don't have nested tables this index will correspond to the actual rowIndex property.

Upvotes: 1

Related Questions