Senger CodeLab 🚀

How to Display Selected Item in Bootstrap Button Dropdown Title

September 29, 2026

How to Display Selected Item in Bootstrap Button Dropdown Title

Crafting dynamic and interactive web interfaces is a cornerstone of modern front-end development, and Bootstrap provides an excellent framework for achieving this with minimal effort. However, a common challenge arises when developers want to enhance the user experience beyond the default static dropdown behavior. Specifically, many seek to understand How to Display Selected Item in Bootstrap Button Dropdown Title, transforming a simple selection menu into a more intuitive, user-feedback-driven component. This seemingly small detail significantly improves usability, allowing users to immediately see their choice reflected directly on the button that triggered the dropdown. We’ll explore the techniques, focusing on practical implementation with JavaScript and jQuery, to make your Bootstrap dropdowns not just functional, but truly engaging and informative for your users.

Understanding Bootstrap Dropdowns and Their Default Behavior

Bootstrap’s dropdown component is a versatile element, perfect for navigation menus, action lists, or selection inputs. By default, a Bootstrap dropdown button typically displays a static title, such as “Dropdown,” “Select an Option,” or a relevant category name. While this static title serves its initial purpose of indicating an actionable element, it doesn’t dynamically update to reflect the user’s current selection. This limitation can sometimes lead to a less intuitive user experience, especially in forms or settings where immediate visual feedback on a chosen option is beneficial.

The core structure of a Bootstrap dropdown involves a button that toggles a list of items (.dropdown-menu). Each item within this list is typically an anchor tag () or a button (

Achieving this dynamic behavior requires stepping beyond Bootstrap’s purely CSS and data-attribute-driven components and integrating a small amount of client-side scripting. This is where JavaScript, often leveraged through the jQuery library due to its prevalence in Bootstrap projects, becomes indispensable. The process involves listening for user interactions and then programmatically altering the button’s text content. This enhancement transforms a basic dropdown into a more interactive and user-friendly control, making your application feel more responsive and intelligent.

  • Static Default: Bootstrap dropdowns initially show a fixed title.
  • Enhanced UX: Dynamic titles improve clarity and user feedback.
  • Scripting Required: JavaScript (typically jQuery) is needed to update the button text.

The Core JavaScript/jQuery Logic for Dynamic Titles

To dynamically update the Bootstrap button dropdown title, the fundamental approach involves capturing the user’s click event on a dropdown item and then programmatically changing the text content of the dropdown button. jQuery simplifies this process significantly by providing concise methods for event handling and DOM manipulation. The key is to target the dropdown items and the dropdown button accurately using their respective selectors.

To display the selected item’s text on the Bootstrap button dropdown title, you must attach an event listener to the dropdown menu items. When a user clicks an item, capture its text content and then update the HTML content of the main dropdown button (often identified by the class .dropdown-toggle or a specific ID) with the captured text. This ensures immediate visual feedback, reflecting the user’s choice directly on the button. This approach is efficient and provides a seamless user experience, making the dropdown more intuitive.

The process typically starts by identifying all the actionable items within your dropdown menu. Using a jQuery selector like .dropdown-menu a allows you to select all anchor tags within any dropdown menu. You then attach a click event handler to these elements. Inside this handler, $(this) refers to the specific dropdown item that was clicked. From this item, you can easily retrieve its text using $(this).text(). Once you have the text, the next step is to locate the parent dropdown button (usually a sibling or a specific ancestor of the dropdown menu) and update its text using methods like .text() or .html(), ensuring the user sees their selection immediately. It’s crucial to prevent the default anchor link behavior if the links aren’t navigating to a new page, using e.preventDefault() inside your event handler.

Step-by-Step Implementation Guide

Implementing a dynamic Bootstrap dropdown title involves a combination of HTML structure and a touch of JavaScript/jQuery. We’ll walk through the process to ensure you can replicate this functionality in your own projects. This example assumes you have Bootstrap CSS and JavaScript, along with jQuery, properly linked in your HTML document. As an experienced web developer, I always recommend ensuring these dependencies are loaded correctly, typically at the end of the <body> tag for performance.

1. HTML Structure for the Dropdown

First, set up your basic Bootstrap dropdown. Note the id attribute on the button, which will be our target for updating its text. Also, consider adding a custom class to your dropdown items if you have multiple dropdowns and want more specific targeting.

<div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false"> Select an Option </button> <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton"> <li><a class="dropdown-item" href="">Option 1</a></li> <li><a class="dropdown-item" href="">Option 2</a></li> <li><a class="dropdown-item" href="">Option 3</a></li> <li><a class="dropdown-item" href="">Another Option</a></li> </ul> </div> 

2. JavaScript/jQuery Code to Update the Title

Now, let’s add the script that listens for clicks on the dropdown items and updates the button’s text. Place this script typically before the closing </body> tag or within a <script> block.

  1. Wrap your code in a document ready function: This ensures the DOM is fully loaded before your script tries to interact with elements.

  2. Target the dropdown items: Select all anchor tags within the specific dropdown menu. In our example, $('dropdownMenuButton + .dropdown-menu .dropdown-item') is a precise selector.

  3. Attach a click event listener: Use .on('click', function(e){...}) to handle clicks.

  4. Prevent default link behavior: Call e.preventDefault(); to stop the `` from being appended to the URL or page from reloading.

  5. Get the selected item’s text: Use $(this).text() to retrieve the text content of the clicked item.

  6. Update the button’s text: Select the dropdown button using its ID ($('dropdownMenuButton')) and Question & Answer :
    I am using the bootstrap Dropdown component in my application like this:

    <div class="btn-group"> <button class="btn">Please Select From List</button> <button class="btn dropdown-toggle" data-toggle="dropdown"> <span class="caret"></span> </button> <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu"> <li><a tabindex="-1" href="#">Item I</a></li> <li><a tabindex="-1" href="#">Item II</a></li> <li><a tabindex="-1" href="#">Item III</a></li> <li class="divider"></li> <li><a tabindex="-1" href="#">Other</a></li> </ul> </div> 
    

    I would like to display the selected item as the btn label. In other words, replace “Please Select From List” with the list item that has been selected(“Item I”, “Item II”, “Item III”).

    As far as i understood your issue is that you want to change the text of the button with the clicking linked text, if so you can try this one: http://jsbin.com/owuyix/4/edit

    $(function(){ $(".dropdown-menu li a").click(function(){ $(".btn:first-child").text($(this).text()); $(".btn:first-child").val($(this).text()); }); }); 
    

    As per your comment:

    this doesn’t work for me when I have lists item <li> populated through ajax call.

    so you have to delegate the event to the closest static parent with .on() jQuery method:

    $(function(){ $(".dropdown-menu").on('click', 'li a', function(){ $(".btn:first-child").text($(this).text()); $(".btn:first-child").val($(this).text()); }); }); 
    

    Here event is delegated to static parent $(".dropdown-menu"), although you can delegate the event to the $(document) too because it is always available.