Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to see if a div has a child with a certain class

People also ask

How would you find a child element with a specific class using jQuery?

children() is an inbuilt method in jQuery which is used to find all the children element related to that selected element. This children() method in jQuery traverse down to a single level of the selected element and return all elements. Here selector is the selected element whose children are going to be found.

How do you check if a div has a child or not?

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 .

How do you check if a div contains a class in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

How do you get children of children in jQuery?

Definition and Usage. The children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.


You can use the find function:

if($('#popup').find('p.filled-text').length !== 0)
   // Do Stuff

There is a hasClass function

if($('#popup p').hasClass('filled-text'))

Use the children funcion of jQuery.

$("#text-field").keydown(function(event) {
    if($('#popup').children('p.filled-text').length > 0) {
        console.log("Found");
     }
});

$.children('').length will return the count of child elements which match the selector.


Simple Way

if ($('#text-field > p.filled-text').length != 0)