How to implement the @click event on a Bootstrap4 dropdown menu in a Nuxt.js project

I am working with a Bootstrap 4 dropdown that I want to customize by changing its button's background color when clicked. However, when I tried to add the click event, the dropdown menu no longer hides. Below is my code:

//template
<div class="dropdown" :class="{ test: test }">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" @click="testing">
        Dropdown button
    </button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
        <a class="dropdown-item" href="#">Something else here</a>
    </div>
</div>

//script
  methods: {
    testing(){
      this.test = !this.test
    }
  }
// css
.isTest{
  background-color: blue;
}

I need help fixing my code. How can I change the background color of the dropdown button without affecting its original functionality?

Answer №1

I have made corrections to your code in a JS fiddle found here. The solution I provided is referenced from a post on Stack Overflow. It seems the style you originally included in your question was not being utilized. To fix this, you can bind a custom style like

:style="test ? clickedBtn : null"
within the button tag and implement the code below. This way, the clickedBtn style will only be applied when test is true.

new Vue({
  el: "#app",
  data: {
    test: false,
    clickedBtn: {
      background: "blue",
      color: "white"  
    }
  },
  methods: {
    testing(){
        this.test = !this.test
      console.log('hasan')
    }
  }
})

The previous answer stands, but if you're looking to customize the style only when the button is focused or when the dropdown is visible, you can override bootstrap's default styles. You can add the following CSS code to your stylesheet. Check out the updated JS fiddle here.

.myBtn:focus{
  background-color: blue !important;
}

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

Angular predictive text selection menu

I'm trying to implement an input field with autocomplete functionality. The goal is to have the input field display dropdown values retrieved from a database as I type in it. I've been attempting to achieve this using angular material, but so far ...

Build a row within an HTML table that is devoid of both an ID and a class

Creating an additional row in an HTML table is a common task, but it can become tricky when dealing with multiple forms and tables on one page. In my case, I have a form that contains only a table element, and I want to move some columns from the first row ...

The Ajax request relies on the initial conditions (then)

My goal is to use the initial function to verify if an item exists and if it can be added to the PHP $_session. The aim is to limit the $_Session to a maximum of 3 items, returning 1 if the item already exists in the session. This enables the favorite fun ...

"Maximizing Data Visualization with Fusion Chart's Caption and Subcaption in

I have been attempting to insert HTML tags and special characters into the fusion chart Caption and subCaption, but it is not working as expected. I followed the instructions in this documentation link and also referred to an older resource, but no succes ...

What could be the reason for my CSS selector with :target not functioning properly?

I tried to implement the instructions I found online for using :target, but it's not working as expected. My goal is to change the background color and font color of #div1 when the first link is clicked, and to change the border of #div2 when the seco ...

Tips for saving a value within a jQuery function for future use in a different function

I have a situation where I receive a value from a jQuery function that I need to pass to another function. How can I achieve this? jQuery.getJSON(url+"&startDate="+startDate+"&endDate="+endDate+"&jobId="+jobId+"&type="+type, function(data) ...

Utilizing multiple dropdown buttons in navigation menu items with Bootstrap 4

Trying to find a solution for having sub menus within the menu items. There are 2 dropdown buttons (Reports and Views) within a menu item that is itself a dropdown item. Clicking on the first button displays the submenu below, but clicking on the second ...

Placing a new item following the child of a different element

How can I use jQuery to add a div inside id123 after sth2, but only for one specific occurrence? I understand how to do it after every sth2, but not just once. <div class="id123"> <div class="a"></div> .. <div class="sth1">< ...

The reflow feature in Highcharts does not properly update the width of the container for High

https://i.sstatic.net/8Shix.gif I am currently working on adjusting the width of Highcharts dynamically to fit its parent container. To achieve this, I have set up a mechanism that listens to changes in the sidebar state and triggers a reflow function whe ...

Steps for creating a fixed menu bar that remains stationary while scrolling

I am facing three challenges: When I hover over my menu links, I want them to become 'bold', but it causes the items on the right to move slightly. How can I prevent this movement? In regard to the dropdown menu on "Two", how can I ensure t ...

Tips for hiding the overflow scrollbar on Microsoft Chrome when there is no content to scroll

Looking for a solution to hide scroll bars in Microsoft Chrome, but only when there is no content to scroll? The current div and styles always show the horizontal and vertical scroll bars, even when the height exceeds the content. <div style="backgroun ...

Is there a proper way to supply createContext with a default value object that includes functions?

As I was creating my context, I set an initial state and passed the necessary functions for useContext. Although this method is functional, I'm concerned it may present challenges in larger projects. Does anyone have suggestions for a more efficient a ...

What steps can be taken to prevent a Javascript Timeout Exception when attempting to launch a new browser window?

Recently, I have encountered an issue while running my test cases on a Linux server. Specifically, when trying to open a new window using Robot Framework, I consistently receive a Timeout Exception. This problem seems to be isolated to the server environm ...

Launching shuttles using HTML and JavaScript

I've been working on an HTML code with JavaScript for a platform validation project. Over the last couple of weeks, I've been diligently coding and testing to ensure everything runs smoothly. However, my code suddenly stopped responding, leaving ...

Determining the clicked node in a JavaScript environment

There are multiple spans with labels. <span class="viewEdit">View and edit class one.</span> <span class="viewEdit">View and edit class two.</span> <span class="viewEdit">View and edit class three.</span> <span clas ...

Animate the transition effect as soon as the block is displayed

Looking to create a smooth page transition using CSS changes with the help of Javascript (jQuery). Initially, one of the pages is set to display none. page1 = $('.page1'); page2 = $('.page2'); page1.removeClass('animate').cs ...

CSS Trick: Applying first-of-type selector to specific instances of a div

I'm currently grappling with how to specify that a 'first-of-type' should only be present on specific instances of a div. For instance, suppose I have a class called ".maintext" and in some cases I want the first paragraph to feature a dropc ...

"Implementing a nested drawer and appbar design using Material UI, along with incorporating tabs within

I am currently working on an app that includes tabs for different files, each of which requires its own drawer and appbar. I found a codesandbox example that is similar to what I am trying to implement. Here is the link to the codesandbox One issue I hav ...

The CSS element is covering up the Xpath element

I am grappling with a challenge in my Selenium testcase as a temporary CSS element is blocking the xpath element I need to click on. During the execution of my testcase in Visual Studio, I encounter an error message that reads: OpenQA.Selenium.ElementCli ...

Expanding background images smoothly during transition with varying image dimensions

I am looking for a way to have my background-image transition smoothly, even when it has a different aspect ratio predecessor. Below is an example demonstrating this stretching effect. toggle = true jQuery(document).ready(function() { jQuery(".button ...