#ActuallyAutistic web dev. Does front of the front-end. Loves perf and minimalism. Prefers HTML, CSS, Web Standards over JS, UX over DX. Hates div disease.
The second one causes slightly less work for CPU to do, although the effect is quite minimal with today's computing power. And the work only happens when the value changes, in this case from user action, so not really a big argument for it.
However it is always a good idea to clean up what you create to the DOM, so in this case by adding the removeEventListener part even if using the second approach.
Thanks for the insights! With these corrections both methods work indeed!
Is there a reason to prefer one over the other?
The second one causes slightly less work for CPU to do, although the effect is quite minimal with today's computing power. And the work only happens when the value changes, in this case from user action, so not really a big argument for it.
However it is always a good idea to clean up what you create to the DOM, so in this case by adding the removeEventListener part even if using the second approach.
Thank you! Makes sense.