Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
how to check in jquery if something not defined

I tried the following code to understand how i can get 0 if the value is not defined.

This below is my try to understand but I am getting same error as described below.

var tbl = $("#tableOccupation")[0] != 'undefined' ? $("#tableOccupation")[0] : 0;
alert(tbl.rows.length);

that if not undefined,

return 0 else return whatever the rows.length is

that is above my try but it is still showing

Uncaught TypeError: tbl is undefined

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your error come from this :

var tbl = $("#tableOccupation")[0] != 'undefined' ? $("#tableOccupation")[0] : 0;

You should check for undefined like this ( by removing quotes surrounding undefined )

var tbl = $("#tableOccupation")[0] != undefined ? $("#tableOccupation")[0] : 0;

undefined is a type, not a string :

console.log(undefined == 'undefined') // Prints false

More cleaner solution would be to just check for undefined like this :

let tbl = $("#tableOccupation")[0];

if (tbl) {
  alert(tbl.rows.length);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Given that the rows collection in a HTML Table element refers to every tr within that table, you can just do this:

let $tbl = $("#tableOccupation");
let numberOfRows = $tbl.find('tr').length;
console.log(numberOfRows);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda