Michael
Michael

Reputation: 13616

How to check in JavaScript if table row with specific index exist?

I have this JavaScript block code:

var trows = table.getElementsByTagName('tbody')[0].rows;  
for(var j = ((pageNumber - 1) * rowsPerPage); j < ((pageNumber) * rowsPerPage); j++)
{
  trows[j].style.display = 'table-row';
}

Before I implement this row:

 trows[j].style.display = 'table-row';

I need to check if table row with scpecific index exists.

How to check in JavaScript if table row with specific index exist?

Upvotes: 1

Views: 2932

Answers (2)

Matt Burland
Matt Burland

Reputation: 45135

Since undefined is falsey, you can just do this:

if (trows[j]) {
    trows[j].style.display = 'table-row';
}

Upvotes: 5

Oskar Hane
Oskar Hane

Reputation: 1882

if(typeof trows[j] !== 'undefined') {
    trows[j].style.display = 'table-row';
}

You can check if the type is undefined.

Upvotes: 2

Related Questions