Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - how to test if selector matches anything?

Say I have the following:

$('#foo')

I'd like to know if that selector matches anything. How do I test for this?

like image 812
StackOverflowNewbie Avatar asked Sep 16 '11 00:09

StackOverflowNewbie


People also ask

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


1 Answers

if ($('#foo').length > 0) {

    // do things
}

should do it

like image 104
David Wick Avatar answered Sep 24 '22 06:09

David Wick