Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove class in typescript

Well I have a menu with li tags and it has an a element has a child for navigation and everything works fine , the problem is that I want to remove all the elements, which has the class seleccionado and add only the li on which I'm clicking. I tried .remove to remove the class. It doesn't work, so is there any other option??

The function removes first all elements with that class and after that it only adds the class to the li, which has been clicked.

clicked(event) {
   var el = document.getElementsByClassName("detectar");
   el.classList.remove("seleccionado");

   if(window.location.pathname){
       event.path[3].classList.add("seleccionado");
   }
}
like image 543
Paul VH Avatar asked May 05 '26 11:05

Paul VH


1 Answers

you should avoid using javascript DOM manipulation in Angular. Check my example below. Angular is very powerfull and does not need to use traditional DOM manipulation

this.showMyClass = true;
clicked(event){
    this.showMyClass = false;
}

in Html

<div [ngClass]="{'myClass': showMyClass}"> </div>
like image 107
Derviş Kayımbaşıoğlu Avatar answered May 09 '26 07:05

Derviş Kayımbaşıoğlu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!