Refactor DOM-related JavaScript code