Adedams Posted on May 7, 2021 • Edited on May 7, 2021 Why are these codes not working? #100daysofcode #javascript #codenewbie #programming I have been trying to add a class with classList.add but it's not working and not throwing any error. Top comments (3) Subscribe Submit Preview Dismiss Collapse Expand Paweł Kowalski Paweł Kowalski Paweł Kowalski Follow Performance, JavaScript, Serverless, and Testing enthusiast. Location Earth, Milky Way Work Senior Performance Engineer at platformOS Joined Dec 25, 2017 • May 7 '21 • Edited on May 7 • Edited Dropdown menu Copy link Hide 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 Expand Adedams Adedams Adedams Follow Hi👋🏽, I am a Frontend Developer. I'm currently learning JS and ReactJs Location Lagos, Nigeria Joined Feb 1, 2021 • May 7 '21 Dropdown menu Copy link Hide 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 Expand Paweł Kowalski Paweł Kowalski Paweł Kowalski Follow Performance, JavaScript, Serverless, and Testing enthusiast. Location Earth, Milky Way Work Senior Performance Engineer at platformOS Joined Dec 25, 2017 • May 8 '21 Dropdown menu Copy link Hide 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. Code of Conduct • Report abuse
Top comments (3)
You have
classListName, notclassList.Also you are trying to do something with
buttons, not specificbuttonin a loop.BTW. Next time try pasting code, not image of code. Ideally in a form of jsbin/jsfiddle/codepen.
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')
});
})
}
Sure thing, here you have working minimal example :)
codepen.io/pavelloz/pen/YzZXzXR?ed...
I dont think you need
displayfunction at all, but since you do, you need to execute it, so evrything inside (including attaching event listeners) occur.