how to add html code in javascript code example

Example 1: how to put javascript in HTML

<script>
  //put your java script here
  </script>

Example 2: how to add elements in javascript html

//adding 'p' tag to body

  var tag = document.createElement("p"); // <p></p>
  var text = document.createTextNode("TEST TEXT"); 
  tag.appendChild(text); // <p>TEST TEXT</p>
  var element = document.getElementsByTagName("body")[0];
  element.appendChild(tag); // <body> <p>TEST TEXT</p> </body>

Example 3: how to add javascript in html

<script src="script.js"></script>

Example 4: JavaScript append HTML

let app = document.querySelector('#app');
app.append('append() Text Demo');

console.log(app.textContent);

Example 5: js create tag

//create div
let div = document.createElement('div');
//create button
let button = document.createElement('button');

Example 6: how to add javascript in html

$(document).ready(function() {
  // add your code here
})

Tags:

Html Example