79547990

Date: 2025-04-01 07:42:14
Score: 0.5
Natty:
Report link

I don't write react, but based on my knownledge on Vue, I think you should do this with states instead of revising the DOM directly.

However, talking about the TS code provided above by @hritik-sharma

Let's update a bit:

// You can indicate type like this
const list = document.querySelectorAll<HTMLElement>('.m-list')

// Avoid using "any", especially when you actually know the type
function activeLink(item: HTMLElement) {
    // Go through the list and remove the class
    list.forEach((listItem) => listItem.classList.remove('active'))

    // Add active class to the clicked item
    item.classList.add('active')
}

// Apply the function to click event
// You actually do not need the parameter "(e: MouseEvent)" inside "addEventListener"
list.forEach((listItem) => listItem.addEventListener('click', () => activeLink(listItem)))
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @hritik-sharma
  • Low reputation (0.5):
Posted by: Oliver