Edit This Code:
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Dropdown Example</h2>
  <p>Click on the Dropdown button below. <strong>event.relatedTarget</strong> returns the element which triggered the dropdown.</p>
  <div class="dropdown">
    <button class="btn btn-primary dropdown-toggle" id="menu1" type="button" data-toggle="dropdown">Dropdown Example
    <span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
      <li role="presentation"><a role="menuitem" tabindex="-1" href="tryit.asp-filename=trybs_ref_js_dropdown_events2&stacked=h.html#">HTML</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="tryit.asp-filename=trybs_ref_js_dropdown_events2&stacked=h.html#">CSS</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="tryit.asp-filename=trybs_ref_js_dropdown_events2&stacked=h.html#">JavaScript</a></li>
      <li role="presentation" class="divider"></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="tryit.asp-filename=trybs_ref_js_dropdown_events2&stacked=h.html#">About Us</a></li>    
    </ul>
  </div>
</div>

<script>
$(document).ready(function(){
    $(".dropdown").on("show.bs.dropdown", function(event){
        var x = $(event.relatedTarget).text(); // Get the button text
        alert("You clicked on: " + x);
    });
});
</script>

</body>
</html>


Result:
Try it Yourself - © w3schools.com
Privacy Policy