Check if a div exists with jquery [duplicate]

Yes, I know this has been asked a lot.
But, it confuses me, since the results on google for this search show different methods (listed below)

$(document).ready(function() {
    if ($('#DivID').length){
        alert('Found with Length');
    }

    if ($('#DivID').length > 0 ) {
        alert('Found with Length bigger then Zero');
    }

    if ($('#DivID') != null ) {
        alert('Found with Not Null');
    }
});

Which one of the 3 is the correct way to check if the div exists?

EDIT:
It’s a pitty to see that people do not want to learn what is the better approach from the three different methods. This question is not actually on “How to check if a div exists” but it’s about which method is better, and, if someone could explain, why it it better?

3 Answers
3

Leave a Comment