Separate the event-handler logic a little, just to avoid "onclick=..."

I've moved the event handler out of the DOM, just for separation of concerns.