I hope all is well,
I want to change the menu from the hamburger to closed when clicked. I just need to target the name attribute on the icon using ionicons.com
<ion-icon name="menu-sharp" class="text-3xl cursor-pointer"></ion-icon>
I can target the element using:
const menu = document.querySelector('ion-icon');
but when i try to toggle or add/remove using:
menu.addEventListener('click', () => {
console.log('clicked')
toggleMenu()
})
function toggleMenu() {
menu.classList.remove('menu-sharp')
menu.classList.add('close-sharp')
navLinks.classList.toggle('top-[14%]')
}
it doesn't work because i am not targeting the name attribute from the HTML above
classListis used to manage the classes of an element. To mutate other attributes either set them directly if available or use setAttribute.menu.setAttribute('name', 'close-sharp')navLinksis defined elsewhere given that the question is about mutating thenameattribute of themenuelement.