0

I have the following HTML structure

<div class="store"><a href="#">GERMANY</a></div>
<div class="store_details">
   <p>Stores in Germany</p>
</div>
</div>
<div class="store"><a href="#">ITALY</a></div>
<div class="store_details">
   <p>Stores in Italy</p>
</div>
</div>
<div class="store"><a href="#">FRANCE</a></div>
<div class="store_details">
   <p>Stores in France</p>
</div>
</div>

What I would like to do is to write a jQuery script that at first will hide the contents of "store_details" div for each store. When a user clicks on a store name (country), the script has to display that "store_details" div and hide the contents of other "store_details" divs.

I managed to do the initial hiding of divs, but I got stuck with the other steps.

Any help is appreciated.

Thanks.

Musa
  • 96,336
  • 17
  • 118
  • 137
Psyche
  • 8,513
  • 20
  • 70
  • 85

4 Answers4

1

Code :

$(document).ready(function(){
    $(".store_details").hide();
    $(".store a").click(function() {
        $(".store_details").hide();
        $(this).parent().next().show();
    });​
});​

Working fiddle

bhb
  • 2,476
  • 3
  • 17
  • 32
0

You should be able to get your answer by searching. Buy anyway, this should get you going

css:

.store_details { display: none; }

js:

$(function(){
    $('.store a').click(function(){ //listen for link clicks
         $('.store_details').hide(); //hide all store details
         $(this).closest('div.store').next('div.store_details').show(); //show the selected detail div
    });
});
Johan
  • 35,120
  • 54
  • 178
  • 293
0

Try this:

$(document).ready(function(){          /* when the page has loaded in the browser... */
   $('.store').click(function(){ /* ...bind a click event to the div element... */
    $('.store').removeClass('store_details'); /* When clicked, remove any 'store_details' class off boxSet elements */
    $(this).next().addClass('store_details'); /* ...and add the 'hilite' class to the clicked element */
});
});
Charles Wyke-Smith
  • 2,479
  • 3
  • 17
  • 16
0

A more robust way (i.e. using children instead of next, correcting markup):

CodePen: http://codepen.io/anon/pen/idcro

$(function(){
    $('.store a').on('click', function(){
        var store_details = $('.store_details').hide();
        var l = $(this).parent().children('.store_details');
        l.show();
    });    
})();
r_31415
  • 8,752
  • 17
  • 74
  • 121