Jquery – How to verify an element exists in the DOM using jQuery

jquery

Typically in JavaScript I do something like the below to verify an element does exist:

if (document.getElementById('lblUpdateStatus')) {
    $("#lblUpdateStatus").text("");
}

But, using jQuery – how can I do the same type of thing?

Best Solution

$ returns an array of matching elements, so check the length property and you're good to go

if ($('#lblUpdateStatus').length) {
    $("#lblUpdateStatus").text("");
}