0

Hello I hope the title is descriptive enough to understand my problem,

I have this HTML code. In <td> i have attached a (click) function. Inside this <td>, I have another div which also has a (click) function attached. When I click the button inside <td> both functions are triggered. But, I would like only the (click) in div to be triggered when I click it and not the <td> (click). Is it possible to happen?

<td mat-cell align="center" *ngFor="let option of options; let opt_indx = index"
    (click)="optionsClicked(opt_indx, i);" id="defaultBadge-wrapper">

    <div *ngIf="!rowCommands[i].defaultSelected[opt_indx]" class="defaultBadge">
        <a class="badge badge-secondary" matTooltip="Click here to set this Option as Default"
            (click)="markDefault(i,option.id)">Default</a>
    </div>
</td>

Typescript code

markDefault(index, option: string) {
    alert('this function triggered');
}

optionsClicked(option, rule) {
    alert('I dont want this function to be triggered when i click markDefault');
}
Antifa
  • 377
  • 1
  • 4
  • 14

3 Answers3

2

In ts method pass the $event as param and include

event.stopPropagation();

or in the click event pass another function:

(click)="markDefault(i,option.id); event.stopPropagation();">Default</a>
Abhishek Gautam
  • 1,617
  • 3
  • 18
  • 29
1

use the stoppropagation to prevent the event from propagating

(click)="markDefault(i,option.id); event.stopPropagation();">Default</a>
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
1

just add $event.stopPropagation() to inner click.

<div *ngIf="!rowCommands[i].defaultSelected[opt_indx]" class="defaultBadge">
    <a class="badge badge-secondary" matTooltip="Click here to set this Option as Default"
        (click)="$event.stopPropagation();markDefault(i,option.id)">Default</a>
</div>

Kamuran Sönecek
  • 3,293
  • 2
  • 30
  • 57