0

Lets say I have two divs: "A" and "B". I want to add "disabled" class to the div "A" as long as the div "B" has an "active" class.

How should I do this with jquery? I tried "hasClass" method but it didn't work.

Bob
  • 169
  • 1
  • 11
  • Please also show what you tried if you want a better explanation as to why it didn't work and how you can improve it. – Spokey Sep 03 '14 at 13:31

4 Answers4

1
if ($('.b').hasClass('active')){$('.a').addClass('disabled')}
Anon
  • 2,854
  • 14
  • 18
0

If you want div "A" to always have a class disabled when div "B" has class active, add a listener. That way, when you add class active dynamically, div "A" is updated too.

$('#parent-of-a').bind('DOMSubtreeModified', function(e) {
    if ($('.b').hasClass('active')){$('.a').addClass('disabled')}
});

https://stackoverflow.com/a/17881562/3460076

Community
  • 1
  • 1
gidomanders
  • 465
  • 5
  • 16
0
if ( $( ".b" ).is( ". active" ) ) {
   $( ".a" ).addClass('disabled');
}
rnd
  • 61
  • 1
  • 3
0

can you try it?

if($(".b:has('.active')") { $('.a').addClass('disabled'); }
Punitha Subramani
  • 1,467
  • 1
  • 8
  • 6