Implementing Style Changes in Angular 2 for Groups of li Elements

<ul>
<li (click)="ChangeColor($event)">ONE</li>
<li (click)="ChangeColor($event)">TWO</li>
<li (click)="ChangeColor($event)">THREE</li>
</ul>

ChangeColor(e){

e.srcElement.style.color="blue"

}

In the list above, when any of the li items are clicked out of the three options, the color of the clicked label should change to blue. When another item is clicked, all item colors should revert back to their original and only the current clicked item's color changes.

Answer №1

Mehdi's advice is to avoid direct DOM access unless absolutely necessary.

Remember, it's best to control your view using data rather than directly manipulating the DOM

Check out my modified and functional code snippet on https://example.com/edit/abc123?p=preview

Answer №2

When working with Angular, it's best practice to avoid directly manipulating the DOM element and instead let Angular handle it for you. For instance, you can create a list using an array defined in your component like this:

export class ExampleComponent {
   links: any;
   activeLink = -1;
   
   constructor() {
      this.links = ['ONE', 'TWO', 'THREE'];
   }
}

Then in your template file, you can display the list like this:

<ul>
   <li *ngFor="let link of links; let i = index" 
       (click)="activeLink = i"
       [ngClass]="{'blue': activeLink === i}">
       {{ link }}
  </li>
</ul>

Make sure to define the CSS class "blue" in your stylesheets:

.blue {
   color: blue;
}

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

Ways to create a menu that appears as fixed-top without moving along with the scroll

When I use the fixed-top class on my nav bar, it stays on top of the header as shown in the image. However, when I scroll down, the menu also scrolls down. Is there a way to make the navigation look the same but not follow when scrolling? I want to achieve ...

The SCRIPT1002 error message popped up, signaling a syntax issue between IE11 and Angular

Encountering an error when trying to open my Angular project in IE11. The error message is as follows: SCRIPT1002: Syntax error vendor.js (224520,1) The line causing the error (224520) looks like this: class PreventableEvent { constructor() { ...

Utilizing Skeleton to create a cropped text button on an iPhone

I am currently using Skeleton for my simple CSS needs. While it works fine on desktop, I have noticed that on an iPhone, the text in my button gets cropped as shown in the picture. https://i.stack.imgur.com/EYo2P.png Below is the HTML code I'm using ...

What is the best way to format code across multiple paragraphs?

I am looking to create a website with an abundance of paragraphs, but I am curious if there is a more efficient method for spacing the content in the code without manually inserting <p> tags for each paragraph. It seems like it may require more than ...

Displaying both the Navbar brand and the mobile dropdown menu on the same line can be achieved

I've encountered an issue where my logo and the mobile dropdown menu won't stay on the same line when the screen size falls below a certain point. I attempted to resolve this by using media queries to resize the image, but unfortunately, the prob ...

How can I customize the appearance of an iframe using CSS?

Simply put, I've uploaded a video to Google Drive that I want to embed on my website. The embed code I got from the shared-video page for the video is: <iframe src="https://drive.google.com/file/d/0B8DK7pele_HjWUR6Ym9KNFU5YTQ/preview" width="640" ...

Angular 4 animation for smooth sliding down

Attempting to add animation to my webpage has presented a challenge: I have a content div on my page, along with a button that triggers the opening of another div above the content. My goal is to have this top div fade and slide into view while simultan ...

The @HostListener in Angular2 does not function correctly within a component that is inherited from another component

My Angular2-based client-side application has a base class: abstract class BaseClass { @HostListener('window:beforeunload') beforeUnloadHandler() { console.log('bla'); } } and two similar derived classes: @Component({ ...

Tips for adjusting the height of Bootstrap Mansory columns

Struggling to figure out how to adjust the height of my column to accommodate three elements in each the first and second columns. As a newcomer to design elements, I could really use some guidance. Here's the mock-up I aim to create: https://i.sstat ...

How should one go about organizing the structure of the DOM?

Whenever I switch the block style to ng-template, my class="icon d-none d-sm-block" doesn't seem to be functioning properly. Any suggestions on how to resolve this issue? <div class="icon d-none d-sm-block" *ngIf="user; th ...

What steps can be taken to ensure that a document in mongodb remains unchanged if it already exists?

As a beginner in the coding world, I am diving into developing an application with SpringBoot that incorporates SpringData MongoDB, Angular, and MongoDB itself. My goal is to create a form submission process where if a user's details are already prese ...

What is the best way to define a function with personalized parameters?

Let me try to explain this the best way I can. Below is a function that generates an HTML element in the #root div: Here is the function: var root = document.getElementById("root"); function createEl(element, selectorType, selectorName, src, in ...

What is the best way to update a BehaviorSubject holding an array without replacing the entire array?

When it comes to adding items to an array BehaviorSubject, the common practice is to assign a copy of the entire array along with the new item using next(). However, I am looking for a way to push items into this array without having to assign a full copy ...

Tips for customizing stepper color using hooks in Material UI

Looking for help to change the color of a stepper from 'primary' to 'success' using React hooks. The stepper component does not have a built-in color method like an icon, so I'm struggling to find a solution. Any advice would be gr ...

Other options besides re-flowing and repainting

After doing some research on various platforms like Stack Overflow, I've come across information stating that re-paints and re-flows can be quite taxing on a browser's resources. I am interested in learning about alternative CSS/JS techniques to ...

Creating Fixed/Sticky Headers for Tables within Tabs in Bootstrap 4

I'm working on a website using Bootstrap 4 and it features multiple tabs with different tables, some of which contain a large amount of data. I am looking to implement a feature where the table header remains fixed at the top of the page as the user s ...

Having difficulty clicking on the logo due to a CSS problem

HTML Code <div class="app-header-logo"> <a href="/home"><img ng-src="img/..."/></a> </div> CSS Styles .app-header-logo { width: 48px; height: 64px; padding: 8px 0px 8px 0px; margin: 0px 16px 0px ...

Is there a way to modify the background of a div when a specific field within my component is true and the div is being hovered over?

When I hover over a div, I want the background color to change. Additionally, I need the color choice to be based on an element within my component. <div *ngFor="let u of users;" [style:hover.background-color] = "u.selected ? 'red ...

Using JavaScript to animate a background composed of SVG shapes

My code is successfully adding an svg background with js and rotating it using set interval. It works perfectly on Chrome, but not on any other browser. Any suggestions on how to make it work universally? let i = 0; setInterval(() => { document.b ...

Struggling to create a basic website design using Flex within Bootstrap 5

As a newcomer to Bootstrap and web development, I am eager to create a simple welcome-page with a specific design in mind. The layout should resemble the image found https://i.sstatic.net/AAEQ4.png. The colors in the image indicate where flex rows could b ...