Scott B
Scott B

Reputation: 40157

jquery to change html order of sibling div elements?

My markup looks like this:

<ul class="menu collapsible">
    <li class='expand sectionTitle home'>
        <a class='clicker' href='#'>Home</a>
        <ul class='acitem'>
            <li class="section">stuff goes here
        </ul>
    </li>

    <li class='expand sectionTitle section1'>
        <a class='clicker' href='#'>Section 1</a>
        <ul class='acitem'>
            <li class="section">stuff goes here
        </ul>
    </li>

    <li class='expand sectionTitle section2'>
        <a class='clicker' href='#'>Section 2</a>
        <ul class='acitem'>
            <li class="section">stuff goes here
        </ul>
    </li>
</ul>

I'd like to add to the jQuery script (see below) that handles the accordian behavior so that when a "clicker" element is clicked, its parent div slides up to the 2nd position. I always want the "Home" div at the top, but I want whichever div is "active" to be expanded in 2nd position.

Here is the jQuery that controls the menu's accordian behavior:

jQuery.fn.initMenu = function() {  
    return this.each(function(){
        var theMenu = $(this).get(0);
        $('.acitem', this).hide();
        $('li.expand > .acitem', this).show();
        $('li.expand > .acitem', this).prev().addClass('active');
        $('li a', this).click(
            function() {
                var theElement = $(this).next();
                var parent = this.parentNode.parentNode;
                if($(parent).hasClass('noaccordion')) {
                    $(theElement).slideToggle('normal', function() {
                        if ($(this).is(':visible')) {
                            $(this).prev().addClass('active');
                        }
                        else {
                            $(this).prev().removeClass('active');
                        }    
                    });
                    return false;
                }
                else {
                    if(theElement.hasClass('acitem') && theElement.is(':visible')) {
                        if($(parent).hasClass('collapsible')) {
                            $('.acitem:visible', parent).first().slideUp('normal', 
                            function() {
                                $('.acitem:visible', parent).first().prev().removeClass('active');
                            }
                        );
                    }
                    return false;
                }
                if(theElement.hasClass('acitem') && !theElement.is(':visible')) {                        
                    $('.acitem:visible', parent).first().slideUp('normal', function() {
                        $(this).prev().removeClass('active');
                    });
                    theElement.slideDown('normal', function() {
                        $(this).prev().addClass('active');
                    });
                    return false;
                }
            }
        }
    );
});
};

$(document).ready(function() {$('.menu').initMenu();});

Upvotes: 3

Views: 3220

Answers (1)

Flo
Flo

Reputation: 1965

Like this?

http://jsfiddle.net/hPZs2/

added

var homeCheckTmp;

after your variable declarations near the top and

homeCheckTmp = theElement.closest('li.expand');
if(!homeCheckTmp.is("li.home")) {
    homeCheckTmp.insertAfter(homeCheckTmp.siblings('.home'));
}

before sliding the element down.

Upvotes: 6

Related Questions