a script block in the page as a second option. <button id="say-hello">Click to Hello</button> <script type="text/javascript”> document.getElementById('say-hello') .addEventListener("click”, function(){ alert('Hello!') }, false); </script>
on run time Anti-pattern: Add the element with ... “HTML strings” & document.write var varWithContent = “paragraph content”; var parrafo = “<p>” + varWithContent + “</p>”; document.write(parrafo); This depends strongly of the context, in some cases, for several insertions to the DOM, like elements in a list, is better to create them with strings and at the final just do one insertion of all the string inserting several elements as one because every manipulation to the DOM affects the performance.