Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda