Is there a way to adjust the background color of the clicked tab with divs and revert the others back to their original color?

<span class="row top-bar-left align-items-center" style="width: 80%; float:left">
        <div tabindex="1" class="link tab" routerLink="details" routerLinkActive="active"
            [queryParams]="activatedRoute.queryParams | async">
            <p>JDA-WMS Details</p>
        </div>
        <div tabindex="2" class="link tab" routerLink="summary" routerLinkActive="active"
            [queryParams]="activatedRoute.queryParams | async">
            <p>JDA-WMS Summary</p>
        </div>
        <div tabindex="3" class="link tab" routerLink="shortage" routerLinkActive="active"
            [queryParams]="activatedRoute.queryParams | async">
            <p>Shortage</p>
        </div>
    </span>

Using the above code, I am implementing three DIVs as tabs. Upon clicking one of them, the background color should change. Subsequently, clicking another tab should trigger a color change and reset the initial tab's background color to its original state.

Answer №1

Utilizing routerLinkActive successfully applies the "active" CSS class to the current tab. To customize this further, a bit of CSS adjustment is all that's required.

.tab.active {
    background-color: blue;
}

Answer №2

If you want to achieve the desired functionality using jQuery, you can do so with just a few lines of code:

$('.tab').click(function() {
$('.tab').removeClass('active');
$(this).addClass('active');
});
.tab {
height:100px;width:100px;background:gray;margin:25px;cursor:pointer;
}

.tab.active {
background:red;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="tab">1</div>
<div class="tab">2</div>
<div class="tab">3</div>

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

What is the best approach to retrieve a JSON object from a form exception that has a specific name?

I have a certain form with input fields. My goal is to generate a JSON object from the form, excluding any input whose name is "point". However, my code doesn't seem to be working properly when I use the "not()" function. What am I doing wrong and how ...

The datepicker feature mysteriously vanishes when using Safari browser

When I utilize the jquery ui datepicker (range) and open my datepicker, if I scroll down the page, the calendar disappears. This issue seems to only occur on Safari browsers with mobile devices. Has anyone else experienced this problem? Would someone be ...

Spooky 'outline' emerges with rounded corners in Internet Explorer 11 and Microsoft Edge

I encountered an unusual issue in IE11 and Edge (on Windows 10) where a strange, transparent border appears in the HTML/CSS code. <!DOCTYPE html><html> <head> <style> body { background-color:red; ...

Get rid of the drop-down shadow effect

Currently working on a website project for a client and looking to remove the blue "shadow" effect from the dropdown menus. I believe the code that needs editing is as shown below: .main_nav ul.sub-menu { position: absolute; top: 65px; width: ...

Tips for converting file byte code to file form data

From one folder I am retrieving a file and uploading it to another server. However, when I extract the file from the first folder, I receive a byte code representation of that file. The API for uploading the file to the second folder requires FormData as a ...

Implement endless scrolling to JSON dataset in an ionic-3 application

Is there a way to incorporate infinite scroll into my JSON array while initially displaying only 5 items? data:[ 0: Object { id: 123, title: "New family member Khjkjh has joined mymily", mm_family_id: 122, … } 1: Object { id: 124, title: "New family mem ...

The problem with -webkit-center in HTML

I have encountered an issue with some code that I wrote. When utilizing -webkit-center and entering text into a textbox, all the items shift to the right. I have attempted other -webkit alignment settings without any problems, only -webkit-center seems to ...

CSS resetting can lead to tables appearing misaligned

After applying a CSS reset to my website's stylesheet (which is valid as CSS 2.0 and used with XHTML 1.0 transitional webpage), I noticed that a table on my website no longer looks correct. Specifically, the table is now positioned incorrectly and the ...

Are browser window.sessionStorage and ng libraries like ngx-webstorage considered safe to implement in Angular 4?

Seeking the optimal approach for utilizing Session Storage in Angular 4 or newer versions such as 5. The current project is making use of HTML5 window localStorage and sessionStorage. Is it advisable to implement third party libraries like angular-2-local ...

What is the best way to eliminate the curved border and inset box shadow from the Material UI TextField component in React?

Check out this input field: Notice the inner shadow and curved borders. Take a look at the given code snippet: import React, {Component} from 'react'; import TextField from 'material-ui/TextField'; import { connect } from 'react ...

Authenticate through Twitter when using PhoneGap Cordova

Looking to implement Twitter login in my application using JavaScript and HTML. How can I redirect users to the Twitter login page when they click on the "Sign In with Twitter" button? ...

Ensuring Proper Alignment of Headers in CSS

I'm struggling to align my header correctly. Despite getting close in Internet Explorer, it looks terrible in Firefox with the code below. I've scoured forums for solutions but the more I try to fix it, the worse it looks. All I want is for heade ...

Using TypeScript to call a class method from within another function

Currently, I am working on an Angular 2 application and struggling to grasp the concept of TypeScript's this scope. Within my TypeScript class named SharedService, there is a function called handleError. If this function encounters a 401 status, I wa ...

Injecting arbitrary text following ?= in a web URL

Consider the following code snippet for a page named MyWebsite.com/page.php <?php $username = "SuperUsername"; $password = "SuperPassword"; if (isset($_GET['p']) && $_GET['p'] == "login") { if ($_POST['user&ap ...

Removing Bootstrap Styles in Angular Single Page Applications

Currently, I am in the process of customizing the styles for an ASP.Net Core 2.2 Angular SPA. Upon examination, I noticed that a specific file webpack:///./node_modules/bootstrap/scss/_reboot.scss is being generated at runtime. To take control of the styl ...

Ways to implement a conditional statement to display a div using JavaScript

Looking for a way to utilize Javascript conditions to toggle the visibility of a div in an HTML5 document? Check out this code snippet below: #demo { width: 500px; height: 500px; background-color: lightblue; display: none; } To set the f ...

Trouble filling a List<WebElement> from a Table using Selenium Java

I am experiencing difficulty filling a List from a table: <div class="es"> <button class="btn btn-primary" [routerLink]="['add']">New User</button> </div> <div class="User_table" id="t02" > <div class="table ...

Update the background color on the sidebar

Objective: I want to change the background of the class="col-sm-3 blog-sidebar" to match the color shown in the image below. Essentially, I am looking to update the sidebar background to reflect the picture's color. Challenge: How can this be ach ...

Designing a unique CSS border for custom list item bullets

I'm currently exploring the possibility of implementing a CSS border around an image that I have imported as a custom bullet for my list items: ul { list-style: none; margin-right: 0; padding-left: 0; list-style-position: inside; } ul > ...

Is it possible to set the radius of a d3.js circle using a style attribute?

Learn more about styling in d3.js with this detailed guide that explains how style attributes can be leveraged to customize the look of a circle, including fill color, stroke color, and stroke width. Is it possible to adjust the radius of a d3.js circle u ...