Is there a way to customize the drop down button color when it is clicked in Bootstrap?

I recently implemented a Bootstrap dropdown button from this source (the second one with an "a" element, turning my button into a link). Everything seems to be working perfectly, except for one thing - I can't get the background color of the button to change when it is clicked. It stubbornly remains the default grey along with the light grey border. My goal is to alter the background color upon clicking the button.

Below is the code snippet:

(Just to clarify, my HTML structure uses li tags as I'm incorporating the dropdown button as a menu item for navigation. Additionally, I have used span instead of div because it helps in proper alignment without forming a block.)

HTML:

<span class="dropdown show">
    <li>
        <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" onclick="myFunction()">Help</a>

    <span class="dropdown-menu" aria-labelledby="dropdownMenuLink">
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
        <a class="dropdown-item" href="#">Something else here</a>
    </span>
    </li>
</span>

CSS:

#color-change {
    background-color: #00ffba;
}

Javascript:

function myFunction() {
    document.getElementById("color-change");
} 

I've searched online and attempted various methods to change the background color on click, but nothing has worked so far. I suspect there might be some interference with the list and/or span elements disrupting the JavaScript functionality, particularly in conjunction with Bootstrap. There's also a possibility that my Javascript code is incorrect. Any insights or solutions would be greatly appreciated!

`

Answer №1

To accomplish this, a simple CSS solution can be implemented by adding a class to the tag.

<a class="btn btn-secondary dropdown-toggle customButton" href="#" role="button"
       id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true"
       aria-expanded="false>Help</a>

CSS

.show > .customButton:focus{
background: #00ffba !important ;
  box-shadow:none !important;
  border: none !important;

}
.customButton{
  box-shadow:none !important;
  border: none !important;
}

If you would like all instances of btn-secondary to have the same color when clicked, you can directly apply the following CSS to that class:

.show > .btn-secondary:focus{
background: #00ffba !important ;
  box-shadow:none !important;
  border: none !important;

}
.btn-secondary{
  box-shadow:none !important;
  border: none !important;
}

Visit this Codepen link for more details:https://codepen.io/anon/pen/xzWrYr

Answer №2

Try out this snippet of code to customize the background color on your website. Remember to update the ID and color values as needed.

$('body').on('click', '#specific-button-id-for-background-change', function(){
    $(this).css('background-color', 'desired-color-code-goes-here');
})

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

Transitioning away from bundled Javascript for local debugging

My current tasks on the gulpfile.js for my frontend app involve a serve task that handles the following: Processing less files Bundling all javascripts into dist/bundle.js Uglifying dist/bundle.js However, this setup made local debugging difficult. To a ...

Encountering a problem where PDF is displaying squares instead of text while using ReactDOM.createPortal to

Currently, I'm working on a React application where I am using createPortal in ReactDOM to open certain components in new windows. Everything seems to be functioning correctly, except when I try to render PDF files. Strangely, everything works fine wh ...

Scrolling and adjusting window size while perfectly displaying images pixel by pixel using php and javascript

I'm currently working on a forum concept that will feature images. I'm looking to have the image displayed at default width and height, but also want users to be able to resize the window to view more of the image as needed. Additionally, I would ...

Trouble with Fetching Data by ID in MongoDB 2.0.0 Driver

In my NodeJS/Express web service project, I am using the MongoDB 2.0.0 driver to interact with the database. UPDATE: Following previous feedback, I have made changes to my code, including removing the db.close() call. Everything works smoothly when I per ...

Accepting user input in a PHP for loop

Almost there, but I'm having trouble with a loop in my PHP code. The issue is that the code doesn't wait for user input and continues on. I've included my code below. The Excel spreadsheet starts at row 4 with multiple choices. Once a choice ...

Unable to set background-image on Node (Limited to displaying only images sourced from Flickr)

I am experiencing difficulty with the background-image node property not functioning properly. In order to test this issue, I am using the "Images & breadthfirst layout" demo as an example (accessible at https://gist.github.com/maxkfranz/aedff159b0df0 ...

Issue with scoring quizzes in NodeJS

Hey there! I've created a quiz website using the "MEN stack" (sans React), and you can check out the repository link here. During local testing, the quiz functions as expected with no issues. The scoring is executed upon form submission, where I comp ...

Steps for displaying an HTML table in Excel by clicking on a button

My goal is to open an HTML table in XLS format with a single click of a button. Currently, I have a JavaScript function that allows me to export the HTML table to XLS using the "save as" option. However, I want to enhance this functionality so that clickin ...

The jQuery webUI popover does not allow for focusing

Encountering an issue with webUI popover functionality. I have 2 links that each open a popover upon click, and I am trying to copy the contents of both popovers without refreshing the page. When I successfully copy the content from one popover and then tr ...

Is there a way to trigger a JavaScript function on button press using C# methods?

Here's my current situation: I have an OnTextChanged event handler on my ASPX page that calls the TextChanged() method from the code-behind. I now need to include a JavaScript script at the conclusion of the TextChanged() method. How can I achieve th ...

A dynamic Angular search box designed for filtering columns in a table

I have a functioning table code that displays data related to domains: Now, I would like to enhance this table by adding a dynamic search box specifically for filtering the column named domain. As the user types in new characters in the search box, the ta ...

When the browser window is resized to mobile view, a div is overlapped by an image

I've encountered an issue with the image size and position when resizing to mobile view in the browser. .extension { display: table; padding: 50px 0px 50px; width: 100%; height: auto; color: #fff; background-color: #558C89; ...

Transforming strings of HTML into objects in the DocX format

After developing a TypeScript script that transforms a JSON string into a Word Doc poster using Docx, I encountered a hurdle. Certain sections of the JSON may contain HTML tags, such as <br/>, <i>, <p>, and I need a way to pass the stri ...

Creating a consistent height for a static div to match a fluctuating dynamic div

Let's discuss a challenge I'm facing with div elements: <div class="middle"></div> On a daily basis, this particular div is populated with API data causing its height to vary. In addition, there are these two other divs in ...

Creating functional dropdown menus with popperjs in the latest Bootstrap version 5

Currently, I am attempting to implement dropdown menus in Bootstrap 5. According to my research, this feature requires Popper.js integration, but I am uncertain of the correct way to include it in my Laravel project that utilizes Laravel Mix. I have made ...

Stop the execution of jQuery ready handlers

I am facing a situation where I need to prevent jQuery ready handlers from firing on the DOMContentReady event under certain conditions. These handlers are set up in different places like include files, plugins, and more. While one approach could involve s ...

Electron / Atom Shell unable to locate specified module

I am completely new to npm, node, and Electron. Here is the structure of my folder: -package.json -index.html -main.js -js/myStuff.js -node_modules In the file myStuff.js, I have a line that says var chokidar = require('chokidar'); but it keep ...

Adaptive video player for complete YouTube experience

Is there a way to incorporate a YouTube video as the background of a div, similar to how it is done on this site, using a <video> tag and ensuring that the video "covers" the entire div without any empty spaces while maintaining its original ratio? ...

Tips for resetting form fields and clearing previous values before resubmitting the form, allowing for a fresh start with empty fields

Welcome to my website! If you visit , you'll notice a feature I've added that clears form field values after submission. However, I'm encountering an issue where the fields remain filled when revisiting the page for another appointment. I at ...

In what way can you set the sequence of properties in a JavaScript object for a MongoDB index while using node.js?

The MongoDB documentation emphasizes the importance of the sequence of fields in compound indexes. The definition of an object in ECMAScript states that it is an unordered collection of properties. When using MongoDB with node.js (such as with this mod ...