I'm currently working on an Angular frontend with TypeScript. I'm trying to get the checkbox values from the array elements, which I'm iterating through in the HTML file with ngFor, and add those values to the total value. The total value should be displayed on the webpage.
I managed to get the value once a checkbox is selected, and to increase the total value according to the checked checkbox value. The logic for decreasing the total value once a checkbox gets unselected is missing though. That's where I need some help.
Unfortunately I could not find the specific implementation of the substraction logic on unselect. Many SO posts included JQuery which I couldn't use, because the JQuery code did not function despite having no errors (I installed Jquery in Angular with npm).
HTML file:
<span name="total" id="grandtotal">estimation total: {{grandtotal}}</span>
<ul *ngFor="let ticket of tickets">
<li>
<span>Estimation: {{ticket.estimation}}</span>
<b>ID: {{ticket.id}}</b>
<input
type="checkbox"
id="ticket.id"
(click)= "calculateTotal(ticket.estimation)">
<li/>
<ul/>
TypeScript file:
totalValue: number = 0;
public calculateTotal(checkboxValue: number): void {
var checkboxes = <HTMLInputElement> document.getElementById('ticket.id');
if(checkboxes.checked) {
this.totalValue = this.totalValue + checkboxValue;
}
}