Consider my element structure
<a href="#" class="close">X</a>
<div class="container">Lorem ipsum</div>
Now on click of the anchor tag I need to hide both the elements,
$('body').on('click', '.close', function(){
$(this).hide();
$('.container').hide();
});
How do I chain both the hide() statements into one?
You can use add() to combine multiple selectors in to a single jQuery object:
$('body').on('click', '.close', function() {
$(this).add('.container').hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<a href="#" class="close">X</a>
<div class="container">Lorem ipsum</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With