getelementsbytagname is returning wrong values

Asked

Viewed 28 times

1

I’m trying to make an HTML table selectable, when I try to get the table rows by getElementsByTagName(), is returning different values when reloading the page.

My Table:

Minhas Tabela:

Javascript:

var tabela = document.getElementById("listSensors");
var linhas = tabela.getElementsByTagName("tr");
console.log(linhas);
console.log(linhas.length);

Returns:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

It seems to me it’s something to do with running time, some suggestion to resolve?

  • About HTML, avoid posting code image. Prefer to post the code in the question. In this link you will understand why :)

1 answer

0


Enter your javascript code before closing the tag </body> to ensure that all HTML content has been loaded. Also, use the event onload to ensure that javascript runs after loading.

window.onload = function() {
    var tabela = document.getElementById("listSensors");
    var linhas = tabela.getElementsByTagName("tr");
    console.log(linhas);
    console.log(linhas.length);
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.