<div id="example"> <div id="test"></div> </div> <div id="another">Blah</div>
I want to set $('#another').hide()
but only if #example
contains a child element called#test
, is this possible? It seems like it would be.
jQuery children() method is used to get the direct children of the selected HTML element. You can use children() method to traverse through the child elements of the selected parent element.
It is a jQuery Selector used to select all elements that are the direct child of its parent element. Parameter Values: parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.
Use length
if ($('#example').find('#test').length) { // found! }
I think what you are looking for is the following
if (jQuery.contains($('#example'), $('#test')) { $('#another').hide(); }
This might work as well, not sure off the top of my head.
if ($('#test', $('#example')).size() > 0) { $('#another').hide(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With