@akpekig
Posted
I think your code could be improved by reducing the redundancy. A lot of the elements are selected using specific classes despite having the same functions and events. To solve this, instead of:
const elementOne = document.querySelector(".element1");
const elementTwo = document.querySelector(".element2");
...
const elementN = document.querySelector(".elementN");
give them a unifying class name that can call them all simultaneously:
const elements = document.querySelectorAll(".element");
Then you can write a function that handles the conditional logic and pass it to the array of elements so you aren't writing the same/similar conditionals repeatedly:
function handleEl(el, optionalEl) {
...
}
elements.forEach(element => {
element.addEventListener('click', () => handleEl(element))
});
Marked as helpful
@byNico1
Posted
@akpekig thanks I did that and probably could have improved my code more, but already made the JavaScript better