How to Modify Button Backgrounds in Angular 8 after Click Events?

Within my Angular 8 Component, I have implemented a row of 4 buttons using the ant design (antd) library in HTML.

My Objective:

  • I aim to modify the background-color of a button when it is clicked.
  • The previously clicked button should revert to its original styling once another button is clicked. The newly clicked button should be highlighted by changing the background-color.

Constraint:

I want to achieve this without utilizing the click-event method. I prefer to accomplish this solely through CSS-based styling, such as assigning buttons with an id and class.

I have reviewed several relevant stack-overflow posts related to this issue but found them unhelpful. Some suggest using hover and active classes.

HTML:

<div class="row">
    <div class="col-md-1 col-sm-1">
      <button (click)="redirectToHub1()" type="button" class="ant-btn ant-btn-primary ant-btn-lg"
        ant-click-animating-without-extra-node="false" ><span>Hub 1</span></button>
    </div>
    <div class="col-md-1 col-sm-1">
      <button (click)="redirectToHub2()" type="button" class="ant-btn ant-btn-primary ant-btn-lg"
        ant-click-animating-without-extra-node="false" >Hub2</button>
    </div>
    <div class="col-md-1 col-sm-1">
      <button (click)="redirectToHub3()" type="button" class="ant-btn ant-btn-primary ant-btn-lg"
        ant-click-animating-without-extra-node="false" >Hub 3</button>
    </div>
    <div class="col-md-1 col-sm-1">
      <button (click)="redirectToHub4()" type="button" class="ant-btn ant-btn-primary ant-btn-lg"
        ant-click-animating-without-extra-node="false" >Hub 4</button>
    </div>
  </div>

Typescript:

 redirectToHub1() {
    this.router.navigate(["/hubs/hub1"], {
      queryParams: { id: `${this.hubCode}` },
    });
  }
 redirectToHub2() {
    this.router.navigate(["/hubs/hub2"], {
      queryParams: { id: `${this.hubCode}` },
    });
  }
redirectToHub3() {
    this.router.navigate(["/hubs/hub3"], {
      queryParams: { id: `${this.hubCode}` },
    });
  }
 redirectToHub2() {
    this.router.navigate(["/hubs/hub4"], {
      queryParams: { id: `${this.hubCode}` },
    });
  }

Current Display:

https://i.sstatic.net/P1Rgs.png

Required Display:

https://i.sstatic.net/UcOk7.png

The below styling works perfectly, but having

(click)="redirectToHub()"
methods disrupts it.

.ant-btn:active, .redirect_btn:focus{
  background: blue;
}

Answer №1

Check out this sample, it can be quite helpful.

.btn {
  border: 1px solid blue;
  background: #e7e7e7;
  outline: none;
}

.btn:active, .btn:focus{
  background: blue;
}
<button class="btn">Test 1</button>
<button class="btn">Test 2</button>
<button class="btn">Test 3</button>
<button class="btn">Test 4</button>
<button class="btn">Test 5</button>

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

The functionality of CSS columns is not supported in Firefox browser

Currently, I am working on a web page design inspired by Pinterest. However, I have encountered an issue where the columns property is not functioning properly in Firefox. You can find the example I am trying to replicate here. Feel free to test it in Fir ...

Toggle the highlighting in React

I have a list of data that displays in a Collapse, and I want it to highlight for every user click in the list, but only one at a time should be highlighted. If you'd like to see a sample to better understand this concept, please check out: https:// ...

`To transfer the selected radio button value to a different form field using jquery, follow these steps.`

I need to set either the value no or 1 for the input field name="auth[]" below. <td> send <input type="radio" name="authorized[]'.$c.'" id="send'.$c.'"value="1" checked> </td> <td> no <input label=" ...

Manipulating a cloned object using jQuery

var clonedForm = $('form').clone(); //clonedForm.find('input[type="hidden"]').remove(); clonedForm.find('select').each(function(){ $($(this).val()).insertAfter($(this)); $(this).remove(); }); Atte ...

"Enhancing Code Readability with Language-Specific Syntax Highlighting

My goal is to develop an editor that allows users to input code in various languages by selecting an option from a dropdown menu. The code should then be automatically highlighted based on the chosen language. I am considering using Codemirror for syntax ...

Custom AngularJS directive for ensuring the selection of a required HTML element

Today brings another question as I continue working on my web application. I've encountered an issue with the 'required' attribute not being widely supported in major browsers. The attribute only works if the first option value is empty, whi ...

AngularJS controller does not trigger ngRoute

I have developed a basic application to experiment with ngRoute functionality. The main page, index.html, contains a single link leading to a separate page named "informationdisplay.html". Despite configuring the route within the controller of the main pag ...

Quicker loading times for background images when creating PDFs using wkhtmltopdf

As I work on generating a 5-page PDF from my HTML file using wkhtmltopdf, everything seems to be running smoothly. However, I've encountered an issue when it comes to the time it takes to complete this task, especially when a background image is inclu ...

The footer moves upwards when a file is downloaded

I am facing an issue with my website footer and its CSS styling. #footer { position: absolute; bottom: 0; width: 100%; height:8rem; } The main goal is to keep the footer at the bottom of the page, regardless of the amount of content on th ...

How to Style Background with CSS on the Server-Side? (Including Links and Hover Effects)

I'm facing a dilemma in choosing between an ASP.NET control and an HTML element for creating a button with a CSS-defined background that changes on hover... 1. Initially, it seems the ASP.NET ImageButton control is not suitable for this purpose becau ...

tips for creating a jquery/css scales animation

I am looking to implement a unique feature on my website where I create scales with a balancing effect. The scales should mimic an up and down motion continuously. You can see an example of what I'm referring to here Is there a way in jQuery or CSS t ...

The issue with the smooth scrolling feature in next/link has not been resolved

I am currently facing an issue where smooth scrolling is not working when using next/Link, but it works perfectly fine with anchor tags. However, the downside of using anchor tags is that the page reloads each time, whereas next/link does not reload the pa ...

Modifying the content of a webpage with the help of Tampermonkey

I'm working on changing a W Text into B using the code below. It's functional, but I'm experiencing two issues. First, there is a lag when refreshing the page where it briefly shows the W text before replacing it with a B. Second, every 20 o ...

What are some ways to create a responsive Grid in React JS with the help of Bootstrap?

Utilizing bootstrap in my react js project, I am implementing css grid to showcase some cards. However, I am encountering responsiveness issues. import React from "react"; import "./Project.css"; export const Project = () => { return ( <div& ...

Switch from flexbox layout to begin on the next column within the same row

I am attempting to create a CSS layout where, after a specific element like :nth-child(6), the elements will break and form a separate column within the same row. The parent element should split into 2 columns after every 6th element in the same row, then ...

Picture not adapting correctly to various screen sizes

I'm looking to adjust the size of an image based on the user's browser window. Here is the code I have so far: .image { max-height: 15%; width: auto; } The image class is used in this section of my code: <ul> <li> <img c ...

Creating a border around an SVG element using the background color of its parent container

I am aiming to layer 2 SVGs on top of each other. The top SVG will have an outline to make it stand out more from the bottom SVG. These SVGs are embedded on buttons with transitions and hover colors. I want the outline SVG to match the background color of ...

Tips for refreshing the 'state' of an Angular Router component

I'm facing an issue with passing data between pages using Angular routing. Everything works as expected when navigating to the "next" page for the first time. However, upon returning to the home page and selecting a different item, the Router's s ...

Sequential Element Loading using jQuery upon pageload

Can someone please explain how to achieve the effect shown on this website: http://www.getflow.com/ ? I believe that jQuery is being used to gradually adjust the opacity of each element. Is there a simple JavaScript snippet available to load each image e ...

How to troubleshoot the error I am encountering in Vue while using custom HTML tags?

While I'm still a Vue newbie, I find it quite enjoyable. I've been experimenting with using just a custom tag like this: <ui-button>Learn more</ui-button> Unfortunately, I encountered an error asking me to register the component. To ...