Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!