DEV Community

Cover image for Why are these codes not working?
Adedams
Adedams

Posted on

1

Why are these codes not working?

I have been trying to add a class with classList.add but it's not working and not throwing any error.

Top comments (3)

Collapse
 
pavelloz profile image
Paweł Kowalski • Edited

You have classListName, not classList.
Also you are trying to do something with buttons, not specific button in a loop.

BTW. Next time try pasting code, not image of code. Ideally in a form of jsbin/jsfiddle/codepen.

Collapse
 
adedams profile image
Adedams

Oh sorry about the presentation. This is actually my first post.

let buttons = document.querySelectorAll('button');

function display() {
buttons.forEach(buttons => {
buttons.addEventListener('click', () => {
buttons.classList.add('active')
});
})
}

Collapse
 
pavelloz profile image
Paweł Kowalski

Sure thing, here you have working minimal example :)

codepen.io/pavelloz/pen/YzZXzXR?ed...

I dont think you need display function at all, but since you do, you need to execute it, so evrything inside (including attaching event listeners) occur.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay