KBlack
KBlack

Reputation: 45

jquery vertical nav - slideUp slideDown

i use this html structure:

    <div id="products">
    <ul>
        <li class="selected"><a href="#">Item 1</a>
            <ul>
                <li><a href="#">Sub-Item 1 a</a></li>
                <li><a href="#">Sub-Item 1 b</a></li>
                <li><a href="#">Sub-Item 1 c</a></li>
            </ul>
        </li>
        <li><a href="#">Item 2</a>
            <ul>
                <li><a href="#">Sub-Item 2 a</a></li>
                <li><a href="#">Sub-Item 2 b</a></li>
            </ul>
        </li>
        <li><a href="#">Item 3</a>
            <ul>
                <li><a href="#">Sub-Item 3 a</a></li>
                <li><a href="#">Sub-Item 3 b</a></li>
                <li><a href="#">Sub-Item 3 c</a></li>
                <li><a href="#">Sub-Item 3 d</a></li>
            </ul>
        </li>
        <li><a href="#">Item 4</a>
            <ul>
                <li><a href="#">Sub-Item 4 a</a></li>
                <li><a href="#">Sub-Item 4 b</a></li>
                <li><a href="#">Sub-Item 4 c</a></li>
            </ul>
        </li>
    </ul>
</div>

and this jquery code:

    $(document).ready(function () {

    $("#products li").click(function() {
        if ($(this).attr('class') != 'selected') {
            $('#products li').removeClass('selected');
            $(this).addClass('selected');
            }
    });


    $("#products li").click(function() {
        $('#products li ul').slideUp();
        $('#products li.selected ul').slideDown();

    });

});

Problem:

i only want to have the slide up and down effect on items of the first level (< li >) not on the sub-items

an other problem - if the list item is already class="selected" and you click on it it slides up and then again down.. (nothing should happen if it is already )

can someone help with the jquery code?

Upvotes: 0

Views: 1162

Answers (3)

Samich
Samich

Reputation: 30095

Try this out:

Demo: http://jsfiddle.net/LEdgT/6/

$(document).ready(function () {
    $("#products > ul > li").click(function() {
        if ($(this).has('ul')) {
            if ($(this).hasClass('selected')) {
                $(this).removeClass('selected')
                       .find('ul').slideUp();
            }
            else {
                $(this).addClass('selected')
                       .find('ul').slideDown();
            }
        }
    });
});

Upvotes: 1

Moin Zaman
Moin Zaman

Reputation: 25435

You can do it with :not selector or .not

    $('#products > li:not(.selected)').click(function() {
        $('#products li ul').slideUp();
        $('#products li.selected ul').slideDown();
    });

Upvotes: 0

Lapple
Lapple

Reputation: 3373

Change your selector to $('#products > ul > li'), it would select only the immediate children.


Or you could change the entire code to something more readable and less repetetive:

var $topLevelItems = $("#products > ul > li");

$topLevelItems.click(function() {
    if (!$(this).hasClass('selected')) {
        $topLevelItems.removeClass('selected')
                      .find('ul')
                      .slideUp();

        $(this).addClass('selected')
               .find('ul')
               .slideDown();
    }
});

Upvotes: 0

Related Questions