Refactoring JS code event listener for symmetry