Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide 'this' and another element using jquery

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?

like image 997
Gibin Ealias Avatar asked Jun 24 '26 02:06

Gibin Ealias


1 Answers

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>
like image 92
Rory McCrossan Avatar answered Jun 26 '26 15:06

Rory McCrossan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!