Skip to content

Instantly share code, notes, and snippets.

@racinmat
Created September 9, 2016 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save racinmat/4a207a12e8ea1851957de7f0cf163687 to your computer and use it in GitHub Desktop.
Save racinmat/4a207a12e8ea1851957de7f0cf163687 to your computer and use it in GitHub Desktop.
appending html with javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>seznam</h2>
<ul>
<li>položka</li>
<li>položka 2</li>
</ul>
<script type="application/javascript">
window.onload = function () {
console.log('onload event triggered');
//using document write after onload creates new html document and wipes out averything rendered before it
// document.write(' <h2>další seznam</h2> ' +
// '<ul> ' +
// '<li>jiná položka</li> ' +
// '<li>jiná položka 2</li> ' +
// '</ul>');
// console.log('html added with document.write');
var div = document.createElement("div");
div.innerHTML = ' <h2>další seznam</h2> ' +
'<ul> ' +
'<li>jiná položka</li> ' +
'<li>jiná položka 2</li> ' +
'</ul>';
document.body.appendChild(div);
console.log('html added with createElement');
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment