Updating Checkbox Appearance in Angular 6 Based on its Checked Status

I have created a checkbox list and I am trying to style the checked item with an underline. Here is my code snippet:

TS file:

currentQuarter: string;
quarters: Observable<MeseRiferimento[]>;
q1: MeseRiferimento = new MeseRiferimento();
q2: MeseRiferimento = new MeseRiferimento();

ngOnInit() {
q1.desc = "One";
q1.id = "1";

q2.desc = "Two";
q2.id = "2"

currentQuarter = q1.id;
quarters.of([q1, q2]);
}

isQuarterSelected(q: MeseRiferimento): boolean {
return this.currentQuarter === this.getKeyFromQuarter(q);
}

HTML file:

<div *ngFor="let q of quarters | async" class="col-1 my-auto m-stati">
<label class="custom-control custom-checkbox ra-check">
<input type="checkbox" class="custom-control-input" [ngClass]="{'checked': isQuarterSelected(q) }">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">{{q.descrizione}}</span>
</label>
</div>

CSS file:

.custom-control-input:checked~.custom-control-indicator {   
color: #fff;   
background-color: #3bb8eb; 
}

Current issues in this code:
1. The default checked checkbox appears correctly with the 'checked' class but the CSS is not applied upon page load, thus it's not underlined
2. When manually selecting a checkbox, the 'checked' class applies and the CSS works appropriately
3. However, when switching from one checkbox to another, the 'checked' class updates correctly but the CSS for the previous checkbox does not update, leaving the previous checkbox underlined

Any advice would be appreciated. Thank you.

Answer â„–1

.custom-control-input:checked~.custom-control-indicator

:checked indicates that the element is selected, not necessarily that it has a specific "checked" class. To target an element with the class "checked", use a dot instead of a colon:

.custom-control-input.checked~.custom-control-indicator

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

Encountering the "DevToolsActivePort file does not exist" error when executing Angular e2e tests via GitHub Actions

Currently delving into the world of GitHub actions, I am in the process of executing Angular e2e tests within a continuous integration workflow. Utilizing the basic project structure generated by the Angular CLI, I have primarily focused on adjusting the ...

Prevent vertical scrolling on touch devices when using the Owl Carousel plugin

Is there a way to prevent vertical scrolling on Owl Carousel when dragging it horizontally on touch devices? It currently allows for up and down movement, causing the whole page to jiggle. If anyone has a solution, I can share the JavaScript file. Appreci ...

Finding the nearest span element with a selector in styled components

Currently experimenting with integrating Styled Components to target the nearest span element. <label> <span className="">Password</span> <input type="password" id="passwordInput" /> </label> ...

Uploading images using Angular and PHP: A comprehensive guide

I am a beginner in Angular and I am having trouble uploading an image from Angular as I encounter 4 errors: 1) Error in the post method: Cannot find name 'formData'. Did you mean 'FormData'?ts(2552) 2) Error in the subscribe method: ...

Despite having the correct image URLs, the images are failing to display in the custom section of Shopify

Currently, I'm in the process of developing a unique Shopify section that showcases text alongside images. To enable users to upload images via the theme customizer, I have implemented the image_picker settings within the schema. The issue I am facing ...

Identifying Angular 2 templates post-file separation: a step-by-step guide

I am currently trying to figure out how to initiate a project in Angular 2 and have encountered an issue. Following the steps outlined in this Angular 2 guide, I was able to separate my .ts files from .js files by configuring my 'temp' directory ...

How to Turn Off Hover Effect in Essential Grid for Mobile Devices

My website, tyloz.com, features four panels on the homepage that act as landing pages. These panels have a hover effect that also works on mobile devices. However, I am looking to disable the hover and double tap functionality specifically on phones. Is th ...

Ensure that test cases in Angular2 include a line that covers conditions for returning values

I'm having trouble implementing test coverage for an online platform, as I'm not sure how to approach it. Within my service method, I have the following code: public getResults() { return this.http(url,body).map(this.mapResponse); } private ...

"Using Angular Material and the cdk library for drag and drop functionality, you can easily drag and drop content into a TextArea field and have

Upon reviewing these examples: https://jsfiddle.net/qskxzh0e/3/ http://jsfiddle.net/3p1nra6m/1/ and https://stackblitz.com/edit/angular-component-drag https://material.angular.io/cdk/drag-drop/overview Is it feasible to incorporate some Token form L ...

Conceal the scrollbar while navigating within the parent container

I'm currently working on implementing a parallax header on my WordPress site using the Divi theme. Below is the code I have so far: <div class="parallax"> <div class="parralax__layer parallax__layer--back"> <img src="https://crisp ...

How can Angular be used to dynamically show or hide an element based on its height?

Is there a way in Angular to display a "Read More" link once the height of a paragraph reaches 200px? I'm looking for an elegant solution. Here are my elements: <section class="mynotes" ng-if="MyController.mynotes"> <p ng-bind="MyController ...

Loading content dynamically into a div from an external or internal source can greatly enhance user experience on a website. By

As I work on my website, I am incorporating a div structure as shown below: <div class="container"> <div class="one-third column"> <a id="tab1" href="inc/tab1.html"><h2>tab1</h2></a> </div> & ...

The margin-top property in CSS is not functioning as intended for a specific pixel value when applied to

I'm having trouble with positioning an icon using margin-top: -35px; under #menu. When I click on the icon, the side navigation bar doesn't work properly. However, if I adjust it to -15px, the side navigation bar displays correctly. Can someone h ...

Angular2: Retrieving the XLS file received from the backend

I am utilizing Laravel 5.1 REST API along with the maatwebsite solution to create an Excel file from the back-end. My main goal is to initiate the download of the file upon button click. Currently, I am making an AJAX request through Angular2's Http s ...

Getting rid of unnecessary compiled CSS files in Compass

After making changes to files and running compass compile, the compiled files remain even if they are renamed or deleted. Similarly, compass clean does not remove these old files as it only focuses on cleaning up current files in use. I want to avoid compl ...

Is there a way to customize the look of the close button on a Bootstrap 5 search box?

I am currently in the process of designing a blog template using Bootstrap 5. While working on creating the search box, I encountered a minor issue. The code snippet below has provided me with a small close button: .input-group-append .btn { border-bot ...

What is preventing my Angular form from displaying the input data correctly?

I've been diligently following a PDF tutorial step by step, but for some reason, I'm not achieving the same results. Despite thorough googling, I can't seem to figure out where I'm going wrong. Here's the content of student.compon ...

What could be causing spacing problems with fontawesome stars in Vue/Nuxt applications?

Currently, I am working on implementing a star rating system in Nuxt.js using fontawesome icons. However, I have encountered an issue where there is a strange whitespace separating two different stars when they are placed next to each other. For instance, ...

Responsive design is achieved through the use of mobile media

I am seeking assistance with optimizing my website for mobile devices as it currently displays like the image below. Here are the solutions I have considered so far: Duplicating the 680 lines of CSS within the same document between @media only screen ta ...

Navigating through a Collection of Pictures using Mouse Movement and Left-click Button

Trying to create a customized PACS viewer for scrolling through a series of CT head images. However, encountering an issue with scrolling through all 59 images. Currently, able to scroll from the first to the 59th image, but struggling to loop back to the ...