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

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

I have a div #popup that is dynamically filled with several paragraphs with the class .filled-text. I'm trying to get jQuery to tell me if #popup has one of these paragraphs in it.

I have this code:

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

Any suggestions?

ベストアンサー1

You can use the find function:

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

おすすめ記事