0

I have custom dropdownlist on jquery and my open-list click works fine, but i need to handle collapse click on button with click on body.

How can i handle a body click event to collapse my list ?

html

<div class="xn-dropdown">
<span class="select-button xn-select" >
    <span>select
    </span>

</span>
<div class="select-menu-wrap">
    <div class="select-menu-modal">
        <div class="select-menu-list">
            <div class="select-menu-item">

                <a href="#">itemnumber 1</a>                    
            </div>
            <div class="select-menu-item">
                <a href="#">itemnumber 1</a>

            </div>
            <div class="select-menu-item">

                <a href="#">itemnumber 1</a>
            </div>
        </div>
    </div>
</div>

javascript

$(document).on("click",".select-button",function() {
$(this).toggleClass('select-button').addClass('selected');
    $('.select-menu-wrap').show();

});



if(!$(".xn-select").hasClass('select-button')){
$(document).on("click","body",function() {    
    $('.xn-select').toggleClass('selected').addClass('select-button');
    $('.select-menu-wrap').hide();
});
}

and fiddle(now works fine!)

http://jsfiddle.net/qMMcN/6/

3 Answers 3

2

here's a modification that should do it

http://jsfiddle.net/XKTsC/

$(document).on("click",".select-button",function(event) {
    $(this).toggleClass('select-button').addClass('selected');
    $('.select-menu-wrap').show();
    event.stopPropagation();
});

$(document).on("click","body",function() {    
    $('.xn-select.selected').removeClass('selected').addClass('select-button').next().hide();
});

You need to prevent the .select-button click event from bubbling up to the body, or the body click handler will undo the changes made by the button click handler

Sign up to request clarification or add additional context in comments.

2 Comments

Nice catch. Both binding to document made me go grrrr :)
What I like to do is register the click function inside the function that shows the menu, but registering it with the one api.jquery.com/one method which means it won't catch every single click just the first click after opening the menu.
0

How about just toggling on clicking the button each time ?

$(document).on("click",".select-button",function() {
    $(this).toggleClass('selected');
    $('.select-menu-wrap').toggle();   
});

http://jsfiddle.net/qMMcN/2/

2 Comments

i need to collapse list on body click !
Here, this is no good (since both bind to document) but it might get you started, i'm off to bed ! jsfiddle.net/qMMcN/4
0

Here's my solution for this;

fiddle: http://jsfiddle.net/qMMcN/5/

just one function for js

var open = false;

$(document).bind("click", function (event) {
    event.stopPropagation();
    var buttonClicked = $(event.target).hasClass('select-button');
    if (!open && buttonClicked) {
        $('.select-button').addClass('selected');
        $('.select-menu-wrap').show();
        open = true;
    } else {
        $('.select-button').removeClass('selected');
        $('.select-menu-wrap').hide()
        open = false;
    }
});

2 Comments

you have problem with second click on button
Works perfectly at chrome. which browser are you using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.