How to add animations to individual items in an Angular list on click

Struggling to animate a single list item upon clicking, I realized the animation trigger is not being accessed in the onclick event code.

A helpful solution I found while searching can be viewed here.

<button mat-flat-button (click)="finishedChore('morning')">
<img [@openClose]="ismorning" src="assets/images/morning.png" />
</button>
<button mat-flat-button (click)="finishedChore('poop')">
<img [@openClose]="ispoop" src="assets/images/poop.png" />
</button>
<button mat-flat-button (click)="finishedChore('cleanRoom')">
<img [@openClose]="iscleanRoom" src="assets/images/cleanRoom.png" />
</button>
<button mat-flat-button (click)="finishedChore('cleanSinks')">
<img [@openClose]="iscleanSinks" src="assets/images/cleanSinks.png" />
</button>
<button mat-flat-button (click)="finishedChore('evening')">
<img [@openClose]="isevening" src="assets/images/evening.png" />
</button>

Creating Boolean variables for each item may become overwhelming with a large number of items or unpredictable quantity.

Is there an alternative method available, or is it possible to access the animation trigger in the code for better control?

Answer №1

To ensure the event is properly handled in your onClick function, pass it as a parameter and specify the id as an example:

<button mat-flat-button (click)="handleEvent($event)" id="exampleId">

This way, you can easily access all necessary information within your .ts file.

handleEvent(event) {
   var element = event.target || event.srcElement || event.currentTarget;
   var idValue = element.attributes.id.value;
}

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

Using Grails, the event listener can be triggered by a change in the

Looking for some advice on implementing a dynamic textfield element in a Grails form. I'm using the remoteFunction action to call a template, but unfortunately, the template is not being called as expected. When I switch from "g:textField" to "g:selec ...

JavaScript versus Non-JavaScript Links and Forms: A Comparison

The other day, I posted a query that got me thinking: Comparing AJAX link with normal link It revolved around the idea of creating links that function for JavaScript-enabled browsers using AJAX, while also providing a normal link for non-JavaScript brows ...

Angular Routing can be a powerful tool for managing multiple article posts in an efficient and organized way

I am in the process of building a website with Angular that features numerous articles. Whenever a user clicks on an article, I want it to navigate to a new URL using routing. To achieve this, I have created a new Article component and here is how my app- ...

What is the process for removing items from an array in JavaScript that share the same values?

If you have an array of objects structured like the following: "medios":[ { "Key":"1", "Text":"Cheque" }, { "Key":"2", "Text":"Tarjeta de Crédito" }, { "Key":"3", ...

The file "tfjs_binding.node" could not be located in the directory where @tensorflow is installed

After attempting to utilize certain functionalities of TensorFlow, I encountered an error indicating that "tfjs_binding.node" was not found in the @tensorflow installation folder. I made sure to install Python 2.7 as a prerequisite for TensorFlow and veri ...

Encountering challenges with periods in URL when utilizing a spring boot application alongside Angular in a live environment

Currently, I am in the process of developing a Spring boot + Angular application using JHipster and deploying it in a docker container with JIB. However, encountering an issue where URLs containing a dot are not functioning properly when accessed directly ...

Struggling to get the jQuery resize event to function properly

Ensuring that my charts remain responsive on different devices has been quite a challenge. Despite implementing a resize event handler in my function to dynamically adjust the charts, I encountered an issue where the page would go blank upon resizing the b ...

Transforming Color with JQuery on the Fly

Check out this Jquery script that gradually changes the color of an object from (0, 0, 0) to (255, 255, 0). $(document).ready(function(){ var r = 0; var g = 0; changeColor(r, g); }); function changeColor(r, g){ var newColor = "(" + r + ", ...

Encountering the error message "Expected undefined to be truthy" while testing the creation of a Component

Recently, I've been tasked with enhancing my skill set by writing Jasmine/Karma tests for an Angular 9 application. After completing an online tutorial and doing some research via Google, I began working on my initial test cases independently. However ...

Guide to inserting .json data into a .js file

Currently, I have an HTML5 banner designed using Flash CC. However, the platform in which I am showcasing this ad does not support JSON files. I am looking for a way to transfer the information from the JSON file into either my .html or .js file so that ...

Setting up web pack with flag-icon-css integration

I have successfully set up a webpack project using https://github.com/teroauralinna/webpack-guide. Now, I am looking to integrate https://github.com/lipis/flag-icon-css into my project. To do so, I followed these steps: npm install flag-icon-css --save T ...

Encountered an error with symbol '@' while utilizing ES6 decorators

I have recently set up a React project and now I'm attempting to integrate MobX into it. This requires using decorators such as: @observable However, when I try to implement this, I encounter the following error: https://github.com/mobxjs/mobx Mod ...

Generate a new random color in Vue.js for each user added to the list whenever the page is refreshed

I am looking to convert some jQuery code into Vue.js. The goal is to assign a random color to each user in a dynamic list every time the page is (re)loaded. Below is the original jQuery code: //RANDOM USER COLOR $(".usersElements").each(function(inde ...

Introducing random special characters into currency symbols during the process of exporting data to a CSV file

I'm encountering a strange issue when exporting data to CSV files that contain a currency symbol. An extra junk character is being added to the data alongside the currency symbol. For example, if my data is: France - Admin Fee 1 x £100 The result I& ...

Conceal or reveal buttons using JavaScript

I am struggling with a function that is supposed to hide certain buttons and create a new button. When I click on the newly created button, it should make the previously hidden buttons visible again. However, the function does not seem to work properly. ...

Guide to encapsulating a container within a map function using a condition in JSX and TypeScript

Currently, I am working with an array of objects that are being processed by a .map() function. Within this process, I have a specific condition in mind - if the index of the object is greater than 1, it should be enclosed within a div element with a parti ...

An error occurred while trying to convert a circular data structure to JSON during an API request within another

Attempting to make an API call within another API call in this code, however encountering the following error: Error: Converting circular structure to JSON const express = require('express'); const router = express.Router(); const config = requi ...

Error Alert: Next.js TypeScript is reporting that the necessary packages are missing from your setup

As I work on developing a basic Next.js website using their TypeScript starter, everything was going smoothly with the 'yarn dev' command. However, out of nowhere, I started encountering an error message whenever I tried to run 'yarn dev&apo ...

Can the keys be extracted from the combination of multiple objects?

Basic Example Consider this scenario type Bar = { x: number } | { y: string } | { z: boolean }; Can we achieve type KeysOfBar = 'x' | 'y' | 'z'; I attempted this without success type Attempted = keyof Bar; // ...

The error message encountered was: "jQuery has not been defined."

I'm struggling to understand why I'm encountering this issue. Take a look at the code snippet below: <head> <script src="https://code.jquery.com/color/jquery.color-2.1.2.min.js" type="text/javascript"></script> ...