Targeting the child of a non-trigger element using jQuery

  Kiến thức lập trình

On click of a button, I’d like to see whether a div has a class of ‘is–active’ and if so, also apply a class of ‘is–active’ to the div’s child.

The challenge I’m having is targeting the div’s child because the div is not the trigger element, so using ‘(this)’ doesn’t work, and I have several instances of the same class so I’d prefer not to give every one a different class name.

Here’s my code:

$(".button").click(function(){
   if ($('.div').hasClass("is--active")) {
      $(this).find('.child').addClass("is--active");
}});

LEAVE A COMMENT