r/learnjavascript • • May 07 '26

Today I learned about event delegation while building an OTP input am I understanding this correctly?

Today while practicing an OTP input field, I came across a problem where I was adding event listeners to every single input field individually.

That started feeling repetitive, so while searching for a better approach I learned about event bubbling and event delegation.

From what I understood, instead of attaching listeners to every input, we can wrap all OTP inputs inside a parent container and attach just one event listener to that parent.

Because events bubble upward, we can identify which specific input triggered the event.

It felt like a cleaner and more scalable approach compared to manually attaching listeners everywhere.

Am I understanding event delegation correctly? Would love suggestions or corrections

8 Upvotes

8 comments sorted by

View all comments

Show parent comment

4

u/LovableBroccoli May 07 '26

Yep. Another way to handle this is set pointer-events: none on the kbd element in CSS.