javascript - jquery parent sibling selector two steps back -
i trying change class of element can't seem right here html
<div class = "grand"> <section class = 'minimized'> <div class = "div-1"> <button class = 'assign-show'></button> </div> </section> <section class = 'assignment'> <div class = "div-2"> <button class = 'assign-hide'></button> </div> </section> </div>
what need if assign-show clicked, add <section class = 'assignment'>
, <section class = 'minimized'>
below jquery script
$('.assign-show').on('click',function(){ $(this).closest('.minimized').addclass('hide'); $(this).closest('.minimized').addclass('assignment-hide'); $(this).parent('section').siblings('.assignment').removeclass('hide'); $(this).parent('section').siblings('.assignment').removeclass('assignment-hide'); });
the above code works addclass remove class not working.
your issue because parent()
travels 1 level dom tree, yet section
2 levels away .assign-show
. fix issue can use either parents()
(note, plural), or closest()
. latter seem better fit stops @ first matching occurrence.
also note can join addclass()
, removeclass()
calls together. try this:
$('.assign-show').on('click', function() { $(this).closest('.minimized').addclass('hide assignment-hide'); $(this).closest('section').siblings('.assignment').removeclass('hide ssignment-hide'); });
Comments
Post a Comment