Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select an element which does not contain a certain child element?

Tags:

<div class="test">  <div class="example"></div> </div>  <div class="test"> </div> 

How can I apply jQuery to an element with the class test only if it doesn't contain a child element with the class example?

like image 507
UserIsCorrupt Avatar asked Apr 16 '12 03:04

UserIsCorrupt


People also ask

Which is used to select that element which does not contain any children?

filter(function() { return !$(

How do you select an element that does not have a specific class?

To select element that does not have specific class with JavaScript, we can use the document. querySelector method with the :not pseudoclass. const li = document. querySelector("li:not(.

How do you select all child elements except last?

When designing and developing web applications, sometimes we need to select all the child elements of an element except the last element. To select all the children of an element except the last child, use :not and :last-child pseudo classes.

How do you know if an element has a child element?

To check if an HTML element has child nodes, you can use the hasChildNodes() method. This method returns true if the specified node has any child nodes, otherwise false . Whitespace and comments inside a node are also considered as text and comment nodes.


1 Answers

$('.test:not(:has(.example))') 

-or-

$('.test').not(':has(.example)') 
like image 106
vol7ron Avatar answered Sep 19 '22 11:09

vol7ron