I have 3 divs of the same class, but with a unique identifier, and when I click on one cell, I would like to hide the other 2.
This is not a problem, and I could achieve this with a few if / elses or case case, perhaps, but I was wondering if there is a more general / efficient way to hide all elements of the same class that are not the one that was clicked?
<div id="boxes"> <div id="box1" class="box">Box 1</div> <div id="box2" class="box">Box 2</div> <div id="box3" class="box">Box 3</div> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('.box').click(function() { $(this).html('hi'); if ($(this).attr('id') == 'box1') { $('#box2').hide('slow'); $('#box3').hide('slow'); } .......... more if }); }); </script>
source share