Having trouble on a simple one. My morning tea isn’t strong enough.
IF div has a child that’s an anchor THEN- blah. Don’t want to add an additional class to .box
Something like:
$('.box').click(function(){
if ($(this).children('a')) {
//some thing
} else {
//some thing else
}
});
<div class="box"><a href="#"><img src="#" /></a></div>
<div class="box"><img src="#" /></div>
<div class="box"><img src="#" /></div>
Check the
lengthproperty:Since the
childrenmethod (like most jQuery methods) returns an instance of jQuery, it will always evaluate totrue(so you’ll always end up in theifbranch). Thelengthproperty returns a number, which will evaluate tofalseif it’s0.