Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle the text in <a> tag in angular 7

I have a button on tag with text "Unfreeze". I want to toggle it to "Freeze" on click on the button. Here is ny code:

<a class="btn btn-primary full-width" (click)="clickFreeze($event)">
                    <i class="fa fa-plus-circle"></i>Unfreeze</a>

private clickFreeze(event) {
    console.log("event.srcElement.childNodes[1].textContent", event.srcElement.childNodes[1].textContent);
    if(event.srcElement.childNodes[1].textContent =='Unfreeze'){
      event.srcElement.innerText="Freeze and Save";
    } else if(event.srcElement.innerText =='Freeze and Save'){
      event.srcElement.innerText="Unfreeze";
    }
}

For some reason it does not work. event.srcElement.childNodes[1].textContent is equal to "Unfreeze" when I console it but it does not enter the if loop.

like image 227
Yogesh Mali Avatar asked Nov 07 '19 10:11

Yogesh Mali


1 Answers

I am not sure why you are using this approach, you can simply toggle text conditionally like this -

.html

<a class="btn btn-primary" (click)="isFreeze = !isFreeze">
  <i class="fa fa-plus-circle"></i>
  {{isFreeze ? "Unfreeze" : "Freeze and Save"}}
</a>

.ts

isFreeze: boolean = true;
like image 75
Pardeep Jain Avatar answered Sep 25 '22 00:09

Pardeep Jain