0

Code:

$(function() {
    return $(".side-menu .nav .dropdown").on('show.bs.collapse', function() {
        return $(".side-menu .nav .dropdown .collapse").collapse('hide');
    });
});

Unintended behavior:

Outer container collapses (probably because before shown, he hide all) sometimes within' these milisecs between show.bs.collapse and shown.bs.collapse.

How do I pass $(".side-menu .nav .dropdown") that is triggering .on(...) to the inner function call, so I could hide the others? Or hide only the ones that is already show?

2
  • 1
    You can reference the item triggering with this inside the callback function to on() (e.g. var $nav = $(this);). Another item of note: drop those return statements. If you added code to the DOM-ready handler after what you have here, it wouldn't run because you are ending execution with return. Also, show.bs.collapse is fired when the element starts to be shown, not after it is shown. You may want to use shown.bs.collapse instead. Commented Jan 2, 2019 at 16:15
  • It's running fine. As I said: this code execution is causing the unintended behavior (so, it's running) when it hides all .collapse's before ending the show to shown (the call is on show as you can see) transition, what causes the outter container to "close" (because there are no items expanded for these milisecs). Commented Jan 2, 2019 at 16:22

1 Answer 1

1

$(function() {
    $(".side-menu .nav .dropdown").on('show.bs.collapse', function() {
        //find all the dropdown, exclude the one that was just
        //shown, and hide them
        $(".side-menu .nav .dropdown").not(this).find('.collapse').collapse('hide');
    });
});

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

1 Comment

Actually this worked. Thanks. And I tried to change the show.bs.collapse to shown.bs.collapse within your suggestion, but my outter container still is closing even with one <li> still showing... so I'll do some more searching in this code I'm reviewing...

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.