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

191
Views
Check if element exists

Possible Duplicates:
Is there an “exists” function for jQuery
jQuery determining if element exists on page

if(tr) is returning true when tr is not an element, how do I check whether it's an element that exists?

var tr = $('#parts-table .no-data').parent();
$('.delete', row).bind('click', function (e) {
  that.delete(e.currentTarget);
});
console.log(tr);
if (tr) //returns true when it shouldn't
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Check its length property:

if(tr.length) {
    // exists
}

if(tr) always evaluates to true because a jQuery object, or any JavaScript Object for that matter, is always truthy.

over 4 years ago · Santiago Trujillo Report

0

I always add this little jQuery snippet at the beginning of my JS files

jQuery.fn.exists = function(){return jQuery(this).length>0;}

This uses the same approach many here have suggested, but it also allows you to access whether or not an object exists like this:

if ( $('#toolbar').exists() ){
    $('#toolbar').load(..., function(){...});
    //etc...
}
over 4 years ago · Santiago Trujillo Report

0

That's because tr is a jQuery object, which is truthy (even when the jQuery object is empty). Use if (tr.length) instead, which will be true when length is not zero, false when it is zero. Or alternately, if (tr[0]).

over 4 years ago · Santiago Trujillo 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!