Bootstrap Tutorial

Bootstrap Fixed Navbar | Sticky Top | Sticky Header Bootstrap CDN 5.3, 5.2, 4.6, 4.3, 3.4, 3.3 (New Links) | Bootstrap CDN 5, 4, 3 File Download Bootstrap 5 Modal Disable Outside Click Bootstrap Horizontal and Vertical Divider Bootstrap Modal Pass data Dynamically Bootstrap Confirm Delete Modal | Popup Bootstrap Add Space Between Rows | Margin Bootstrap Remove Modal Background Bootstrap Make Table Row Clickable Bootstrap Change modal background color Bootstrap Center Align Form Bootstrap Show Image in Modal | Popup Bootstrap Button Icon with Text Bootstrap Scrollable Modal Bootstrap Image Center Align Bootstrap Dropdown with Icon Bootstrap Get Checkbox checked value Bootstrap Uncheck radio Button Bootstrap Set Radio button default checked or selected Bootstrap Radio Button Validation Bootstrap Enable Disable Radio Button Bootstrap Radio Button Checked Event Bootstrap get radio button checked or selected value Bootstrap Disable Enable Button on Click Bootstrap Tooltip on Input Field Elements Bootstrap Fixed Footer Bootstrap Image Upload | file Upload Bootstrap Modal on page load Bootstrap Login Form Template Bootstrap Add Search icon with Search bar Bootstrap button tooltip on hover Bootstrap Icons CDN Latest Version (1.11.3) Bootstrap Tabs Panel Pass dynamic content Bootstrap Change Dropdown Menu Background Color Bootstrap Avoid Dropdown Menu to close menu items on clicking inside Bootstrap Table Remove Border Bootstrap Modal Example Bootstrap Center Align Input Field Bootstrap Change Icon Color Bootstrap Text Align Center | Middle Bootstrap Remove all Rounded Corners Bootstrap Alerts Example Bootstrap Alert with Icons Bootstrap Scroll to Top Button Bootstrap Button Loading Spinner Bootstrap Check if Modal is Shown or Hidden Bootstrap Dropdown Button Change Text on Selection Bootstrap Remove offcanvas Backdrop Bootstrap Textarea Height Auto Resize Bootstrap selectpicker Get Selected Value Glyph Trash Icon

Bootstrap Dropdown Button Change Text on Selection

Bootstrap provides a versatile framework for building responsive and visually appealing web applications. One common requirement is to dynamically change the text of a dropdown button based on the user's selection. In this tutorial, we'll explore a simple yet effective method to achieve this using JavaScript within a Bootstrap environment.

Profile Photo

Abhishek Yadav (SD) SDE of FAI

Feedback

written
Profile Photo

Anil Kumar (Expert) Coding Expert of FAI

Feeback

reviewed
Profile Photo
Nov 27, 2023 02:11 AM Last Updated
updated

How to Display Selected Item in Bootstrap Button Dropdown?

The HTML structure lays the foundation for our dropdown button. The button itself and the associated dropdown menu are defined within a container. The dropdown button initially displays the default text, "Select an option," and the dropdown menu contains a list of items with associated onclick events triggering the changeText function.

Update Bootstrap Dropdown Button Text with Selected Item

Copied to Clipboard
Run

In this example, the dropdown button is created using the button element with the class btn btn-warning dropdown-toggle. The list of items is created using the ul element with the class dropdown-menu. Each item in the list is created using the li element with the class dropdown-item. The onclick attribute of each item calls the changeText function with the selected item as the argument.

The changeText function updates the text of the dropdown button with the selected item. It does this by setting the textContent property of the button element to the selected item.

That’s it! Now, when a user clicks on an item in the dropdown list, the text of the dropdown button will be updated with the selected item

JavaScript Code

Copied to Clipboard

Output of Above Example

Pasted Image

The jQuery script in the code snippet targets all elements with the class “dropdown-item” and listens for a click event. When a user clicks on an item, the script retrieves the data-value attribute of the clicked item and updates the text of the dropdown button accordingly. This ensures that the text of the dropdown button is dynamically changed to the selected option whenever a dropdown item is clicked.

Jquery Code

Copied to Clipboard
Run

Output of Above Code

Pasted Image

Ad