Refactor DOM manipulation into directive