refactoring javascript save action