Switch to pure DOM API instead of jQuery.

Fix issues from review.
Move DOM node creation to a function.