2

I have multiple dropdowns (more than 10), and my click method never executes... Is it possible to define same click method to all dropdown elements like I have done below?

JS

<link rel="stylesheet" href="Scripts/bootstrap/css/bootstrap.min.css" />
<script src="Scripts/jquery-2.1.0.min.js"></script>
<script src="Scripts/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">

    $(document).ready(function () {

        $(".dropdown-menu li a").click(function () {

            console.log("inside"); //this never executes...

            var type = $(this).data("type");
            var nick = $(this).data("nickname");

            if (type == "1") {
                $('#chat_text').val('!tip ' + nick + ' ').focus();
            }
            else if (type == "2") {

            }
            else if (type == "3") {
                var val = $('#chat_text').val();
                $('#chat_text').val(val + nick ).focus();
            }
        });
    });
</script>

HTML

<li class="dropdown">
   <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Test1</a>
   <ul class="dropdown-menu" role="menu"> 
   <li><a href="#" data-type="1" data-nickname="Test1">Tip</a></li> 
   <li><a href="#" data-type="2" data-nickname="Test1">Show</a></li> 
   <li><a href="#" data-type="3" data-nickname="Test1">Paste</a></li> 
   </ul> 
   <span>Some text</span>
</li>

<li class="dropdown">
   <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Test2</a>
   <ul class="dropdown-menu" role="menu"> 
   <li><a href="#" data-type="1" data-nickname="Test2">Tip</a></li> 
   <li><a href="#" data-type="2" data-nickname="Test2">Show</a></li> 
   <li><a href="#" data-type="3" data-nickname="Test2">Paste</a></li> 
   </ul> 
   <span>Some text</span>
</li>

More of this dropdowns are in real code....

*EDIT: I forgot to mention that I add these dropdowns dynamically after the page is created (AJAX calls....)

2
  • 2
    nickname must be nick ! you might be getting reference error for the same Commented Jun 3, 2015 at 9:21
  • Thanks. Fixed that but it doesn't get so far in code... Commented Jun 3, 2015 at 9:25

2 Answers 2

6

If you add them dynamicly, try this:

$("body").on('click', '.dropdown-menu li a', function () {

Instead of

$(".dropdown-menu li a").click(function () {

With this change the click handler will be there for every anchor element within the dropdown-menu class, no matter when it will be created.

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

Comments

0

As your content are being downloaded using AJAX calls, you need to implement Event Delegation. Good article is here.

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.