0

I am not able to change class when clicking on the button.

I am using this.

event.srcElement.classList.remove("unsubscribe");
event.srcElement.classList.add("subscribe");

I am passing the event in function when clicking on the button.

<a #subscribe title="unsubscribe" *ngIf="rec?.attributes?.Subscription == 'true'" class='unsubscribe'  (click)="subscribeUnsubscribe($event);$event.stopPropagation();" data-toggle="modal"></a>
<a #subscribe title="subscribe" *ngIf="rec?.attributes?.Subscription == 'false' || rec?.attributes?.Subscription == null || rec?.attributes?.Subscription == 'undefined'" class='subscribe'  (click)="subscribeUnsubscribe($event);$event.stopPropagation();" data-toggle="modal"></a>

The problem is I am having two angular components in one component the srcElement is working fine and toggling the classes, but in another component, it is not working and giving the below error.

ERROR TypeError: Cannot read property 'srcElement' of undefined
4
  • 1
    Can't you use [ngClass] for it? Commented Jan 24, 2020 at 8:31
  • @SunnyParekh, no I want to achieve this with srcElement it is working in another component Commented Jan 24, 2020 at 8:35
  • is it in your directive.ts file? Commented Jan 24, 2020 at 8:35
  • 2
    @JayeshVyas There is no real reason to use the classList. You have such an amazing framework and you use plain javascript dom? Commented Jan 24, 2020 at 8:36

3 Answers 3

1

I don't know the reason why it gives you an error, but this code is not enough to know really. Have you considered using the NgClass directive? Here a tutorial

You can do something like this:

Component:

public subscribed: boolean;

Template:

<div #someElement [class.subscribe]="!subscribed" [class.unsubscribe]="subscribed"></div>
Sign up to request clarification or add additional context in comments.

Comments

0

Ok so you can do something like this:

subscribeUnsubscribe(event) {
 event.srcElement.classList.remove("unsubscribe");
    setTimeout(()=>{
      event.srcElement.classList.add("subscribe");
    },0)
}

I hope it helps.

2 Comments

Have you used timeout?
Can you create a sample on stackblitz to help us understand your concern?
0

Use ngClass to check all your conditions and return your style class. For example:

<a #subscribe [ngClass]="getstyleClass(rec)" title="unsubscribe" (click)="subscribeUnsubscribe($event);$event.stopPropagation();" data-toggle="modal"></a>

and in your component file

public getstyleClass(rec: any):string {
 if (rec?.attributes?.Subscription === true){
  return "unsubscribe";
 }
 if (rec?.attributes?.Subscription == 'false' || rec?.attributes?.Subscription == null || rec?.attributes?.Subscription == 'undefined') {
  return "subscribe"; 
 }
 return "";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.