Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 13, 2023 02:03
Show Gist options
  • Save code-boxx/7aa94533ef194d1fe641e8af57496b84 to your computer and use it in GitHub Desktop.
Save code-boxx/7aa94533ef194d1fe641e8af57496b84 to your computer and use it in GitHub Desktop.
Javascript Dynamic Content

JAVASCRIPT DYNAMIC CONTENT

https://code-boxx.com/display-dynamic-content-html/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Inner and Outer HTML</title>
</head>
<body>
<!-- (A) HTML PARAGRAPHS -->
<p id="first">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
<p id="second">
Vivamus semper eleifend lectus, sed faucibus massa cursus a.
</p>
<!-- (B) REPLACE HTML -->
<script>
window.addEventListener("load", () => {
// (B1) REPLACE THE ENTIRE <P> ELEMENT WITH <STRONG>
var first = document.getElementById("first");
first.outerHTML = "<strong>FOO BAR!</strong>";
// (B2) WILL STILL BE A <P>, BUT CONTENT WILL BE CHANGED.
var second = document.getElementById("second");
second.innerHTML = "<u>FOO</u> <i>BAR</i>";
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Create HTML Elements</title>
</head>
<body>
<!-- (A) HTML -->
<div id="first">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
<!-- (B) CREATE & INSERT ELEMENT -->
<script>
window.addEventListener("load", () => {
// (B1) CREATE NEW HTML ELEMENT
var element = document.createElement("strong");
element.innerHTML = "FOO BAR!";
// (B2) ATTACH TO <DIV>
document.getElementById("first").appendChild(element);
// (B3) TO ATTACH NEW ELEMENT TO <BODY>
element = document.createElement("p");
element.innerHTML = "Lorem ipsum dolor sit amet.";
document.body.appendChild(element);
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>AJAX Load Contents</title>
</head>
<body>
<!-- (A) HTML CONTAINER -->
<div id="container"></div>
<input type="button" value="AJAX Load" onclick="aload();">
<!-- (B) AJAX LOAD -->
<script>
function aload () {
fetch("3B-contents.html")
.then(res => res.text())
.then(txt => document.getElementById("container").innerHTML = txt);
}
</script>
</body>
</html>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
<!DOCTYPE html>
<html>
<head>
<title>AJAX load JSON data</title>
</head>
<body>
<!-- (A) HTML CONTAINER -->
<div id="container"></div>
<input type="button" value="Load Data" onclick="aload();">
<!-- (B) AJAX LOAD JSON DATA -->
<script>
function aload () {
// (B1) AJAX LOAD JSON DATA - BUILD HTML TABLE
fetch("4B-dummy.json")
.then(res => res.json())
.then(data => {
var table = "<table>";
for (let person of data) {
table += `<tr><td>${person.name}</td><td>${person.email}</td></tr>`;
}
table += "</table>";
document.getElementById("container").innerHTML = table;
});
}
</script>
</body>
</html>
[{"name":"John Doe","email":"john@doe.com"},{"name":"Jane Doe","email":"jane@doe.com"},{"name":"Josh Doe","email":"josh@doe.com"},{"name":"Juliet Doe","email":"juliet@doe.com"},{"name":"Joy Doe","email":"joy@doe.com"}]
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Load CSS</title>
</head>
<body>
<!-- (A) DUMMY HTML -->
<div id="container">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
<input type="button" value="Load CSS" onclick="aload();">
<!-- (B) INSERT CSS -->
<script>
function aload () {
var link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = "5B-style.css";
document.head.appendChild(link);
}
</script>
</body>
</html>
html, body {
font-family: arial, sans-serif;
padding: 0;
margin: 0;
}
#container {
padding: 10px;
font-size: 1.5em;
background: #ffe0db;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment