Tips for placing the caret in a Bootstrap dropdown

I need help positioning the caret to the right so that it remains in place when I select an option.

$('#dropdownMenu span').on('click', function() {
                $('#dropdownMenuButton').html($(this).text());
            });
#dropdownMenuButton {
  border: solid 1px black;
  background-color: white;
  box-shadow: none;
  width: 30vw;
  text-align:left;
}

#dropdownMenu {
  height: 50vh;
  overflow: hidden;
  overflow-y: auto;
  transform: translate3d(0px, 38px, 0px)!important;
  width: 30vw;
}
<!DOCTYPE html>
<html>

<head>
  <title>bootstrap dropdown</title>
  <link rel="stylesheet" href="https://www.sailwbob.com/bootstrap/bootstrap.min.css">
</head>

<body>
  <div class="dropdown">
    <button class="btn btn-light dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown"  aria-haspopup="true" aria-expanded="false">
                    Select boats
                </button>
    <div id='dropdownMenu' class="dropdown-menu" >
      <span class='dropdown-item'>Avrora</span>
      <span class='dropdown-item'>Irlbach</span>
      <span class='dropdown-item'>Laura</span>
      
      (more boat options...)
      
    </div>
  </div>
  <script src="https://www.sailwbob.com/bootstrap/jquery-3.3.1.slim.min.js"></script>
  <script src="https://www.sailwbob.com/bootstrap/bootstrap.bundle.min.js"></script>
</body>

</html>

Answer №1

Here is an addition to the code:

.dropdown-toggle::after{ margin-top:1vw; float:right; }

$('#dropdownMenu span').on('click', function() {
                $('#dropdownMenuButton').html($(this).text());
            });
#dropdownMenuButton {
  border: solid 1px black;
  background-color: white;
  box-shadow: none;
  width: 30vw;
  text-align:left;
}

#dropdownMenu {
  height: 50vh;
  overflow: hidden;
  overflow-y: auto;
  transform: translate3d(0px, 38px, 0px)!important;
  width: 30vw;
}

.dropdown-toggle::after{
    margin-top:1vw;
    float:right;
}
<!DOCTYPE html>
<html>

<head>
  <title>bootstrap dropdown</title>
  <link rel="stylesheet" href="https://www.sailwbob.com/bootstrap/bootstrap.min.css">
</head>

<body>
  <div class="dropdown">
    <button class="btn btn-light dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown"  aria-haspopup="true" aria-expanded="false">
                    Select boats
                </button>
    <div id='dropdownMenu' class="dropdown-menu" >
      <span class='dropdown-item'>Avrora</span>
      <span class='dropdown-item'>Irlbach</span>
      <span class='dropdown-item'>Laura</span>
      <span class='dropdown-item'>Lola </span>
      <span class='dropdown-item'>Syrena</span>
      <span class='dropdown-item'>Thea</span>
      <span class='dropdown-item'>Anne</span>
      <span class='dropdown-item'>Bebe</span>
      <span class='dropdown-item'>Camilla</span>
      <span class='dropdown-item'>Emma</span>
      <span class='dropdown-item'>Katy</span>
      <span class='dropdown-item'>Nelli</span>
      <span class='dropdown-item'>Winter</span>
      <span class='dropdown-item'>Aeolus</span>
      <span class='dropdown-item'>Amorina</span>
      <span class='dropdown-item'>Melina</span>
      <span class='dropdown-item'>Nieva</span>
      <span class='dropdown-item'>Nora</span>
      <span class='dropdown-item'>Rush</span>
      <span class='dropdown-item'>Alex</span>
      <span class='dropdown-item'>Blue J</span>
      <span class='dropdown-item'>Corker</span>
      <span class='dropdown-item'>DaCapo</span>
      <span class='dropdown-item'>Freedom</span>
      <span class='dropdown-item'>Golden</span>
      <span class='dropdown-item'>Montana</span>
      <span class='dropdown-item'>Moonshadow</span>
      <span class='dropdown-item'>Phoebe</span>
      <span class='dropdown-item'>Ra</span>
      <span class='dropdown-item'>Rigel</span>
      <span class='dropdown-item'>Santuzza</span>
      <span class='dropdown-item'>Sorccerer</span>
      <span class='dropdown-item'>Stiletto</span>
      <span class='dropdown-item'>Tony</span>
      <span class='dropdown-item'>Wasabi</span>
    </div>
  </div>
  <script src="https://www.sailwbob.com/bootstrap/jquery-3.3.1.slim.min.js"></script>
  <script src="https://www.sailwbob.com/bootstrap/bootstrap.bundle.min.js"></script>
</body>

</html>

Answer №2

Make sure to position it correctly on the right side using absolute positioning

#dropdownMenuButton {
    border: solid 1px black;
    background-color: white;
    box-shadow: none;
    width: 30vw;
    text-align: left;

    /* make sure to add relative positioning */
    position: relative;

}
#dropdownMenuButton::after {
    display: inline-block;
    margin-left: .255em;
    vertical-align: .255em;
    content: "";
    border-top: .3em solid;
    border-right: .3em solid transparent;
    border-bottom: 0;
    border-left: .3em solid transparent;

    /* don't forget to include these lines of code */
    position: absolute;
    right: 5px;
    top: 50%;
    transform: translateY(-50%);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When programming with PHP and JavaScript, managing events' start dates and end dates

In my current project, I am developing a script that deals with events. An event is scheduled to start on 12/02/2016 at 11:20 and end on 01/04/2016 at 8:20. When the end date is reached, I need to trigger a specific action using JavaScript. if (t.getFullY ...

Save the HTML elements in an object and then use the ng-include directive to include the template

Currently experimenting with this code snippet: var CustomTable = function($elem) { this.properties.element = $elem; this.initialize(); }; CustomTable.prototype.PROPERTIE = { secondElement: $('.second') // this element ...

Dynamically assign properties to Vue components using programming techniques

Imagine a situation where there is a component <x> that is intentionally designed without knowledge of a certain property a, but it needs to be able to support and configure this property in any way needed. const env = { props: { a: Number } } < ...

How is it that deferred callbacks in JavaScript do not lead to race conditions?

I came across this snippet from the IndexedDB documentation and wanted to write something similar: var req; var store = getStore(); req = store.count(); req.onsuccess = function(evt) { console.log("success: " + evt.result); }; req.onerror = function(evt ...

What is the method for retrieving the name of a canceled file?

When a user clicks on the "Cancel" button during an upload, I am attempting to retrieve the filename of the file that was canceled. However, it seems like the current method I am using (var image_file_name) is not successfully retrieving the name of the ca ...

The button press for scrolling to the top of each section in JQuery is not functioning as expected

Struggling to get the scroll animation to work for each section when a button is pressed. After researching extensively, I found this code snippet: $('ul.nav').find('a').click(function () { var $href = $(this).attr('href' ...

The system displayed an 'Error' stating that the variable 'index' is defined in the code but is never actually utilized, resulting in the (no-unused-vars) warning

I have configured eslint and eslint-plugin-react for my project. Upon running ESLint, I am encountering no-unused-vars errors for every React component in the codebase. It seems like ESLint is not properly identifying JSX or React syntax. Any suggestions ...

Angularjs pledged to utilize $http for its functionality

$http({ url: '/verifyUserName', data:{username:$scope.username}, method: "POST" }) .then(function(response) { $scope.allowGameStart = true; }) Is there a way to access the variable $scope.allowGameStart outside of the promise in ...

What could be causing the script to display the object's content inaccurately?

Below is the code for the client side: import {useEffect,useState} from 'react'; import io from 'socket.io-client'; import Peer from './Peer'; export default function TestMeeting(){ let peerName; const [peerList,setPee ...

Is there a way to incorporate the Bootstrap 4.0.0 Collapse feature into an HTML table?

Hi there! I have a question about adding collapsible elements to an HTML table. Below is my code: <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5b7babaa1a6a1a7b4 ...

static footer within fluid identical div/columns

After conducting extensive research on the internet and exploring various Stack Overflow questions, I could not find a solution to my problem... MAIN CONCEPT: My challenge involves creating dynamic divs within the body of my webpage, each containing ...

Looking for a responsive solution to automatically scroll a horizontal grid to a specific position when the page loads

Eight years ago, a user asked a similar question on this platform: DIV Horizontal scroll, how to onload to ID The answer provided was: document.getElementById('foo').scrollLeft = 500;​ However, I am currently working on a Bootstrap 4 website ...

"Trouble arises when dealing with nested object arrays in Formik and handling changes in a child

I am struggling with passing a value to handleChange in formik validation. To address this issue, I created a component whose quantity is dynamically added based on the number of numChild. My goal is to allow users to click an icon and add any number of sk ...

``Is there a more SEO-friendly option instead of using an iframe?

I am looking for a solution to easily share my content with other websites without the issues I currently face. Presently, I use an iframe which poses two problems: <iframe width=“540”; height=“700” frameborder=“0” src=“http://www.energi ...

Tips for sending a function with arguments in a React application using TypeScript

Is there a way to streamline passing a handleClick function to the son component so that it does not need to be repeated? The code in question is as follows: Mother Component: const Mother = () => { const [selectedOption, setSelectedOption] = useSt ...

Encasing functions within brackets - `(function() { })()`, and utilizing `.call()`

After spending some time writing JavaScript, I decided to delve into CoffeeScript. One thing that caught my attention is how CoffeeScript compiles functions in the following format: (function() { }).call() I've also come across functions written th ...

How to retrieve a nested element from a multidimensional array using Angular

Need help with Angular - I'm working on a json file containing an array called people[], which in turn has an array named phones[] I aim to extract and display: people[index].phones[index].phonenumber (assuming people.personid = x and people.phones ...

Manipulating JSON Objects in AngularJS

While I may be proficient in utilizing existing data, my expertise in editing JSON objects is not quite up to par yet. Any assistance in this area would be greatly appreciated. This question may seem basic, but I am seeking clarification as I am unsure. H ...

Generate a new text document in Google Drive using JavaScript

Currently, I am working on a web application using JavaScript and jQuery. My goal is to extract notes from an input text field on my webpage and save that information in a text file on Google Drive. Although I have successfully managed to sign in and autho ...

Express.js applications may encounter issues with static files and scripts not being found when utilizing dynamic endpoints

I've run into a snag with my Express.js application regarding the inability to locate static files and scripts when accessing certain endpoints. Here's what's happening: My Express.js app has multiple routes serving HTML pages along with st ...