Reputation: 380
$(function () {
if($('body').find('#slideshow')) {
$('body').find('.topBox').addClass('home');
}
});
I mean it works, but if i take out #slideshow
it will still add the class?
I tried else remove class.
Upvotes: 0
Views: 166
Reputation: 890
if($('body').find('#slideshow')) {}
will always evaluate as true. $('body').find('#slideshow') does return something: an object (even if its an empty object). Instead, test the length of the object:
if($('body').find('#slideshow').length) {}
Upvotes: 1
Reputation: 943569
The return value from jQuery('body').find(...)
will always be true as it returns a jQuery object.
You want to check if it returns any elements that match, so you want:
if(jQuery('body').find(...).size())
Upvotes: 1