I'd like, to jQuery, understand if an element exist into another element.
Somethings like :
if($('#container').find('.search_element'))
must return YES if .search_element
is into #container
, NO otherwise.
How can I do it? Tried with $.contains('#container', '.search_element') but seems that this function doesnt works...
check the length of the array
jQuery has a default method for this:
http://api.jquery.com/has/
A simple length check:
¡¡¡UPDATE!!!
See my answer [ HERE ] for a MUCH more robust plugin!
You could easily shorten @Chris answer with:
You could also do the same with a useful plugin i made for this:
jsFiddle
Of course this plugin could be further extended to be much more fancy (handling multiple calls at once, creating non-existing elements based on a pram), but as it stand now, it does a very simple, very needed function ... Does this element exist? return
True
orFalse
jsFiddle
You can use
.is
and:has
, just to be complete. I would prefer a solution which tests.length
though.