Bob
Bob

Reputation: 10775

Twitter Bootstrap dropdown determine what element is clicked

I have a following code:

<div id="dropdown" class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
      Dropdown
      <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">
      <li><a href="#">Dropdown link 1</a></li>
      <li><a href="#">Dropdown link 2</a></li>
    </ul>
  </div>

I am clicking on some link. How can I determine what link is clicked?

$("#dropdown").click(function(e) {
  console.log(e);
  //how?? 
});

I am also using jQuery.

Upvotes: 0

Views: 75

Answers (1)

Tricky12
Tricky12

Reputation: 6822

You should be able to use this:

$('.dropdown-menu > li > a').on('click', function(e) {
    e.preventDefault(); //Disable href
    window.alert($(this).text()); //$(this).text() is what you want
});

Update: Demo here

Upvotes: 1

Related Questions