0

I think I may know the answer to this, but being still fairly new to the world of jQuery I figure I will ask people much smarter than I. My research on the topic led me to the code I have pasted below but it does not work.

Here is the scenario: I have multiple div/ul based dropdowns that have the same class name of course. What I want to happen is when the button for the dropdown is clicked, that you can click inside of the dropdown element without it closing the dropdown. I am assuming that because they all have the same class, the stopPropogation is not working. Here is the jQuery code:

<script type='text/javascript'>
$('.dropdown-menu').each(function(){
    $(this).click(function(event){
        if($(this).hasClass('keep_open')){
         event.stopPropagation();
        }
     });
});
</script>

Here is an example of one of the dropdowns (of which there are more than one). I use Twitter Bootstrap in case you notice the similar class names:

<div class="btn-group">
    <div class="btn btn-mini" tabindex="-1">DROPDOWN NAME</div>
    <div class="btn btn-mini dropdown-toggle" data-toggle="dropdown" tabindex="-1">
        <span class="caret"></span>
    </div>
    <ul class="dropdown-menu dropdown-pad keep-open">
        <li><b>Info:</b> <small>NAME INFO</small></li>
        <li><b>Thoughts:</b> <small>THOUGHT INFO</small></li>
        <li><b>Favorite Places:</b>
            <br><small>FAVORITE PLACE<br>FAVORITE PLACE 2</small>
        </li>
    </ul>
</div>

EDIT So the error is not likely in the jQuery portion, but something in the dropdown div itself. On the same page I use the following variation of the dropdown and it works like a champ:

<div class="btn-toolbar">
    <div class="btn-group">
    <a class="btn dropdown-toggle" href="#" data-toggle="dropdown">
      Department
      <span class="caret"></span>
    </a>
    <ul class="dropdown-menu keep_open">
       <li>Customer Service</li>
       <li>Tech Support</li>
    </ul>
    </div>
</div>
6
  • Where is the close listener attached? Commented Apr 19, 2013 at 14:05
  • There isn't in twitter bootstrap, it's using CSS pseudo class i think Commented Apr 19, 2013 at 14:07
  • @Vatev what do you mean by "close listener"? Commented Apr 19, 2013 at 14:16
  • @roasted I am not sure you completed your thought :) Either that or I did not understand your comment. I do have my own class added in there: 'keep-open'. I utilize this function and this class on another page and it works. I am concerned this does not work because of so many with the same class name. Thanks again for your help. Commented Apr 19, 2013 at 14:17
  • Where is the function which closes the thing attached. event.stopPropagation() will prevent the event from reaching it only if your listener gets called before it does. Also if it's attached at the same element you need to use stopImmediatePropagation. Commented Apr 19, 2013 at 14:22

5 Answers 5

1

Not sure it is related to your problem, but you should wrap your code in document.ready callback function:

$(function(){ //or $(document).ready(function(){
    $('.dropdown-menu').each(function(){
        $(this).click(function(event){
            if($(this).hasClass('keep_open')){
             event.stopPropagation();
            }
         });
    });
 });
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for noticing that one, I would of probably lost half an hour looking for the reason.
@roasted I appreciate the info on this. I did add it in, but to confirm your suspicion, it did not fix the issue :(
1

Wrap in a document ready but this:

$('.dropdown-menu').each(function(){
    $(this).click(function(event){
        if($(this).hasClass('keep_open')){
         event.stopPropagation();
        }
     });
});

could be simply this:

$('.dropdown-menu').click(function (event) {
    if ($(this).hasClass('keep_open')) {
        event.stopPropagation();
    }
});

final version:

$(document).ready(function(){
    $('.dropdown-menu').click(function (event) {
        if ($(this).hasClass('keep_open')) {
            event.stopPropagation();
        }
    });
});

alternately:

$('.btn-group').on('click', '.dropdown-menu', function (event) {
    if ($(this).hasClass('keep_open')) {
        event.stopPropagation();
    }
});

EDIT: based on comments you MIGHT need:

$('.btn-group .dropdown-menu .keep_open').children().on('click', function (event) {
    event.stopPropagation();//or the next one
    event.stopImmediatePropagation();
});

EDIT2 OMG I am an idiot:

$('.btn-group').on('click', '.dropdown-menu', function (event) {
    if ($(this).hasClass('keep-open')) {
        event.stopPropagation();
    }
});

'keep-open' vs 'keep_open'

7 Comments

tried both the final version and the alternate version... still not working
Please describe "still not working" in detail - perhaps you need one of the other propagation management? i.e. event.stopImmediatePropagation() documented api.jquery.com/event.stopImmediatePropagation
My sincere apologies. I am notoroious for not being as descriptive as I need to be. by "not working" I mean that when the button is clicked, and the dropdown appears, clicking inside the dropdown area closes the dropdown still. It needs to stay open no matter where inside the dropdown I click.
and I did try the stopImmediatePropagation and it produced the same result, i.e. closes the dropdown when clicking inside of the dropdown.
I added another possibility - the children might be managing the click - hard to tell but something you might work with - NOT a complete solution as you MIGHT need to detect the propagation stoppage in the parent (original) to properly manage.
|
0

For multiple elements and live event, use .on() attached to an element below the level of document.body:

$(document).on('click', '.dropdown-menu', function(e){
    $(this).hasClass('keep_open') && e.stopPropagation(); // This replace if conditional.
});

Don't need use $(document).ready()

Comments

0

I figured it out. Thanks to @Mark Schultheiss, it eventually clicked what was going on. The original jQuery worked, however, the first type of dropdown which did not work had a class of keep-open instead of keep_open. That is why the second style of dropdown worked and not the first one. Tested it and it works now. Thank you all for your help!

Comments

0

Having the same problem as this user but these solutions are not working: Twitter Bootstrap cant stop a dropdown from closing on click

Decided to open up a separate question rather than this old one in case the issue is different for newer builds.

Comments

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.