Why duplicate all this code here?  Why not just include some <script>s that add this from what you wrote above?