TransWikia.com

Detect if Class has an element inside of certain Class?

Stack Overflow Asked by ohval on November 17, 2021

I’m trying to get an Alert if a class contains an element of a certain class.

My attempt: https://jsfiddle.net/d9qecgjx/1/

HTML

<div class="parent">

  <div class = "child1">
  </div>
  
  <div class = "child2">
  </div>
  
  <div class = "child3">
  </div>
  
  <div class = "child4">
  </div>

</div>

jQuery

if ( $( ".parent" ).hasClass( ".child3" ) ){
                alert("You have that");
}

One Answer

One approach is to use .find() - it searches descendants. so

if ( $( ".parent" ).find( ".child3" ).length ){
                alert("You have that");
}

Answered by Nikkorian on November 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP