Search code examples
jquerythisselectorsiblings

how to target the sibling of the element that has a same class as the element itself


im trying to hide the sibling of an element but the sibling has the same class as the element itself

here is the jquery code and html code

<div class="a">this is the trigger</div>
<div class="a">this div is to be hidden</div>

$(document).ready(function(){
   $(".a").click(function(){
          $(this).html('this is triggered');
          $(this).siblings(".a").hide();
   });
});

i thought that the this woulg be able to do it for me

Note: for reasons these two divs(.a)must be of the same class

also note the divs are repeated


Solution

  • I think all you need is next()

    $(this).html('this is triggered');
    $(this).next().hide();