Rewrite javascript DOM manipulation as a template

The template is slightly less code, and feels cleaner.