Skip to content

Instantly share code, notes, and snippets.

View djD-REK's full-sized avatar
🌞
Full-Stack JavaScript Developer & Doctor of Physical Therapy 🌞

Dr. Derek Austin djD-REK

🌞
Full-Stack JavaScript Developer & Doctor of Physical Therapy 🌞
View GitHub Profile
// Replace the inline styles:
for (div of document.getElementsByTagName("div")) {
div.style.cssText = "font-size: 3rem"
}
// Clear out all inline styles:
for (div of document.getElementsByTagName("div")) {
div.style.cssText = ""
}
// As a one-liner:
for (div of document.getElementsByTagName("div")) {
div.style.fontSize = "3rem"
}
// Apply CSS styles to all of the selected <div> elements
for (div of allDivs) {
div.style.fontSize = "3rem"
}
// Select all the <div> elements on the current page
const allDivs = document.getElementsByTagName("div")
// Select all <div> elements on the page
const divs = document.getElementsByTagName("div")
// Apply CSS styles to the selected <div> elements
for (div of divs) {
div.style.border = "3px solid orange"
}
// As a one-liner:
for (div of document.getElementsByTagName("div")) { div.style.border = "3px solid orange" }
// Load jQuery, equivalent to having the <script> tag on the page
// <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
const loadScript = async (url) => {
const response = await fetch(url)
const script = await response.text()
eval(script) // or Function(script)
}
const scriptUrl = "https://code.jquery.com/jquery-3.5.0.js"
loadScript(scriptUrl)
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
$.getScript("script.js") // Load a script using jQuery
// <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
const loadScript = async (url) => {
const response = await fetch(url)
const script = await response.text()
Function(script)
}
const scriptUrl = "https://code.jquery.com/jquery-3.5.0.js"
loadScript(scriptUrl)
// <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
fetch("https://code.jquery.com/jquery-3.5.0.js")
.then((response) => response.text())
.then((text) => eval(text))
.then(() => {
/* Use jQuery */ $("div").css("border", "3px dotted orange")
})