Rich
Rich

Reputation: 512

Showing all the LIs in UL, instead of show only the clicked LI.

I'm pretty new to javascript / jquery - I've got a list of stockists in nested ul's , and I want to show / hide the region (revealing all the stockists in that region) . it does show and hide, but all of the regions instead of the one I want - any help greatly appreciated as always

my markup

{foreach $regions index region}

<ul class="stockists">

    <li>
        <div class="regionHeader">

            <span class="regionTitle">{$region.label}</span><span class="regionView">View</span>

        </div>

        <ul class="region">
            <li>

            {$stockists = $region.stockists}

            {foreach $stockists index stockist}

                        <ul>
                        <li class="stockistName">{$stockist.name}</li>
                        <li>{$stockist.address1}</li>
                        <li>{$stockist.address2},{$stockist.city}</li>
                        <li>T : {$stockist.telephone}</li>
                        <li>W : {$stockist.website}</li>
                        <li><span class="productsButton">Products Stocked</span><span class="mapButton">View on Map</span></li>
                        </ul>

            {/foreach}

            </li>

        </ul>           

    </li>

</ul>                  


{/foreach}

my javascript

var stockists = {

start: function() {

    $('.region').hide();
            $('.regionTitle').click(function(e) {
                e.preventDefault();
                $('.region').slideToggle(200);
            })

}

};

$(stockists.start);

thanks for looking, Rich :)

Upvotes: 2

Views: 89

Answers (3)

Rich
Rich

Reputation: 512

I've sorted it, I needed to go up one element, before I could go next ... corrected the line

$(this).parent().next('.region').slideToggle(300); // where .parent is the containing element of .regionTitle, which is div with class regionHeader.

thanks for the help though guys, @gaurav got me looking in the right place :)

Upvotes: 0

Gaurav Pandey
Gaurav Pandey

Reputation: 2796

Try:

var stockists = {
start: function() {

$('.region').hide();
        $('.regionTitle').each(function(){
        $(this).click(function(e) {
            e.preventDefault();
            $(this).next('.region').slideToggle(200);//here is the change basically it will show/hide only the region next to the clicked regionTitle.
        });
        });

}
};
$(stockists.start);

Upvotes: 2

amir
amir

Reputation: 13

the code:

$('.region').slideToggle(200);

toggle all the elements with class 'region'. $('.region') select all the elements with class 'region' if you want to toggle only one element you should select him only add use the function.

Upvotes: 0

Related Questions