I want to test if an element with a specific class name exists or not.
Here's my code:
<div class="album-tracks">
<div class="track">
<div class="play-status">
<span class="add-to-list">
<i class="fa fa-play" aria-hidden="true"></i>
</span>
</div
><div class="track-num">1</div
><div class="track-name">A Head Full Of Dreams</div
><div class="track-time">03:44</div
><div class="track-artist">Coldplay</div>
<!-- <div class="track-album"></div> -->
</div>
</div>
JS
$('.track').dblclick(function() {
var $prev;
$prev = $(this).parent('div').find('.status-active');
if ($prev != null) {
//This will always be true
}
}
And the picture below is the screenshot of the console
I'm confused by the layers of the object. It means that even if I did't get the object with the class that I want, I still get the object with the parent element. So the object is not null, and the result will always be true?
I have found the solution to fix the mistake is that I need to use the length
property.
if($prev.length){
//This works!
}
Just want to clarify the idea that when can I test the result using null value? I'll be appreciate if you give me some advice!!