Instructions on how to deactivate or unselect a toggle switch by simply clicking on the icon among several icons

In my angular application, there are several icons with a toggle switch on the right side. The default state of the switch is ON. When an icon is clicked, its color changes from white to red. By clicking on the switch, the colored icon reverts back to white, and vice versa. This means that clicking on an icon turns the switch OFF (unchecked) and clicking on the switch changes the colored icon back to its original white color.

Below is the code snippet:

.component.html

 <label class="rating-switch" id="toggleSwitch">
      <input class="rating-checkbox" type="checkbox" checked >
      <div class="slide round" >

      </div>
    </label>    <span class="no-rating-switch" >No Rating</span>

<div class="container">
        <span class="iconss"></span><i (click)="selectedIcon = icon.id" class="stl" [ngClass]="icon.class"
  [style.color]="selectedIcon === icon.id ? '#FF0000' : '#ffffff'" *ngFor="let icon of iconsArray"></i>
</div>

.component.ts

 iconsArray = [
    { id: 1, class: "icon-onlife-smiley-face-1" },
    { id: 2, class: "icon-onlife-smiley-face-2" },
    { id: 3, class: "icon-onlife-smiley-face-3" },
    { id: 4, class: "icon-onlife-smiley-face-4" },
    { id: 5, class: "icon-onlife-smiley-face-5" },
    { id: 6, class: "icon-onlife-smiley-face-6" }
]
selectedIcon = 0;

.component.css

.rating-switch{
  position: relative;
  width: 48px;
  height: 17px;
  margin-top: 3em;

}
.switch, .rating-checkbox { 
  opacity: 0;
  width: 0;
  height: 0;
}
.slide {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}
.slide:before {
  position: absolute;
  content: "";
  height: 17px;
  width: 17px;
  left: 1.2px;
  bottom: 2px;
  top:0.5px;
  background-color: #2b547e;
  -webkit-transition: .4s;
  transition: .4s;
}
.rating-checkbox:checked + .slide {
  background-color: #87d3f8;
}

.rating-checkbox:focus + .slide {
  box-shadow: 0 0 1px #87d3f8;
}

.rating-checkbox:checked + .slide:before {
  -webkit-transform: translateX(29px);
  -ms-transform: translateX(29px);
  transform: translateX(29px);
}
.slide.round {
  border-radius: 34px;
}.slide.round:before {
  border-radius: 50%;
}

My goal is to have the switch turn OFF when an icon is clicked, and have the colored icon return to white when the switch is clicked. Any help would be appreciated.

Looking forward to your assistance. Thank you.

Answer №1

<label class="rating-switch" id="toggleSwitch">
    <input class="rating-checkbox" type="checkbox" [(ngModel)]="rating">
    <div class="slide round">

    </div>
</label> <span class="no-rating-switch">No Rating</span>

<div class="container">
    <span class="iconss"></span>
    <i (click)="selectedIcon = icon.id" class="stl" [ngClass]="icon.class"
        [style.color]="(selectedIcon === icon.id && !rating) ? '#FF0000' : '#ffffff'"
        *ngFor="let icon of iconsArray"></i>
</div>

typescript file for the above code

rating = true;
iconsArray = [
    { id: 1, class: "icon-onlife-smiley-face-1" },
    { id: 2, class: "icon-onlife-smiley-face-2" },
    { id: 3, class: "icon-onlife-smiley-face-3" },
    { id: 4, class: "icon-onlife-smiley-face-4" },
    { id: 5, class: "icon-onlife-smiley-face-5" },
    { id: 6, class: "icon-onlife-smiley-face-6" }
]
selectedIcon = 0;

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 checkbox filtering for tables

I have a table populated with data that I want to filter using checkboxes. Below is the HTML code for this component: <div><mat-checkbox [(ngModel)]="pending">Pending</mat-checkbox></div> <div><mat-checkbox [(ngModel ...

The extensive magnetic scrolling functionality in Ionic 2 sets it apart from other frameworks

Hi everyone, I could really use some assistance! I've been working on developing an Ionic 2 App and my navigation setup is not too complex. I have a main menu where clicking on an item opens another menu with a submenu. From there, if I click on an i ...

What is the best way to refresh an array in a different component?

It all starts with a data response object: let response = { attachments: [{id: 1}, {id: 2}, {id: 3}] } This valuable data is utilized in the root component <app-root></app-root> <app-documents [response]="response"></app- ...

Problems encountered with Bootstrap navbar-nav while utilizing ngFor

Greetings, I am currently utilizing bootstrap v4.0.0 in my project. I have noticed that when displaying menus using navbar-nav and implementing ngFor, the bound property seems to be called continuously - is this the intended behavior? Below is a snippet o ...

Serverless-offline is unable to identify the GraphQL handler as a valid function

While attempting to transition my serverless nodejs graphql api to utilize typescript, I encountered an error in which serverless indicated that the graphql handler is not recognized as a function. The following error message was generated: Error: Server ...

Unable to modify an attribute due to TypeScript error: Type 'string' cannot be assigned to type 'never'

I am trying to modify an attribute of an object in TypeScript and React, but I keep encountering the following error message: Type 'string' is not assignable to type 'never'. This happens even though I have a check in place to verify th ...

Issue with ngx-extended-pdf-viewer: text not appearing due to incorrect cmaps path

I have been using the most recent stable version of ngx-extended-pdf-viewer (4.1.2) along with Angular 9.1. Everything was functioning perfectly, but after building the application, certain PDF files were not displaying any text. Upon investigation, it was ...

Angular 13: Masking User Input with Reactive Form Controls

Looking to incorporate a phone number input field with formatting in a reactive form. The desired format is (123) 456-7890. For reference, check out this example link: https://stackblitz.com/edit/angular13-reactive-form-validation-y1qwmf?file=src/app/app. ...

Creating Test Cases for Service Response Validation

I am currently attempting to unit test an API within my ngOnInit method. The method is responsible for making a call to the service in order to fetch details. If the details are not undefined, an array called 'shoeDataResponse' of type *shoeData ...

How can debugging in Chrome be achieved using Typescript?

How is it possible to debug TypeScript in Google Chrome when the browser only understands JavaScript? I find myself debugging my TypeScript files within my Angular project, which was created using Angular CLI, through the Chrome developer tools. However, ...

Encountering a Typescript issue with the updated Apollo server version within a NestJS application

After upgrading my nestJS application to use version 3.2 of apollo-server-plugin-base, I encountered two TypeScript errors related to a simple nestJS plugin: import { Plugin } from '@nestjs/graphql' import { ApolloServerPlugin, GraphQLRequest ...

Angular with NX has encountered a project extension that has an invalid name

I am currently using Angular in conjunction with nx. Whenever I attempt to execute the command nx serve todos, I encounter the following error: Project extension with invalid name found The project I am working on is named: todos. To create the todos app ...

Angular2 emitter does not function properly when using classes in different files

The code provided in this Plunker is functioning well, even when modified with subscribe in the beta version 50. However, it fails to work when the classes are placed in separate files and exported. Is there a way to store the classes in separate files a ...

The ESlint extension in VScode encountered compatibility issues on the MacBook Pro 2021 powered by the Apple M1 Pro chip

After using eslint in vscode on Apple M1 Pro chips, I encountered an issue. I was able to execute eslint via the command line without any problems: > eslint app.js /playground/nodejs/eslint-demo/app.js 1:7 error 'a' is assigned a value ...

The placeholder text in the matInput field is not being displayed

As a newcomer to Angular, I am facing a challenge that has left me unable to find a solution. Below is the code snippet in question: <mat-form-field> <input matInput placeholder="ZIP" style="color:red;"> </mat-form-field& ...

The specified property cannot be found within the type 'JSX.IntrinsicElements'. TS2339

Out of the blue, my TypeScript is throwing an error every time I attempt to use header tags in my TSX files. The error message reads: Property 'h1' does not exist on type 'JSX.IntrinsicElements'. TS2339 It seems to accept all other ta ...

Tips for creating a star program using Angular 2+

Create an Angular 2+ code snippet that will print asterisks (*) in a list on button click. When the button is clicked, it should add one more asterisk to the list each time. For example: Button Click 1 - Output: * Button Click 2 - Output: ** Button Cl ...

How do I utilize the file handler to execute the flush method within the Deno log module using Typescript?

I'm having trouble accessing the fileHandler object from my logger in order to flush the buffer to the file. This is the program I am working with: import * as log from "https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_emai ...

The ngIf statement in the template isn't functioning properly after a refresh; instead, it is causing a redirection to the homepage

I've been developing with Angular 7, trying to display a <div> ... </div> based on multiple values that I declared as : Boolean = false; in the .ts file. These values are updated in ngOnInit, but for some reason, the page keeps redirecting ...

Avoiding the use of destructuring for undefined values in JavaScript can be achieved by implementing

Upon receiving the response registryReportSettings from the server: this.getRegistrySettings(registry.Id).subscribe((registryReportSettings: { extended: ReportPropertiesRequest }) => { const { objectProperties, reportProperties, textProperties } = reg ...