Managed to have JS separate from HTML. The trick is
-top svg element should be created in html
-and the JS should be wrapped around ondocument ready