Kay Ashaolu
<body> <h1>Test</h1> <script src= "../js/script.js" type="text/javascript" ></script> </body>
html/index.html
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" href="../css/style.css" /> </head> <body> <div id="title" class="heading"> Info Web Arch Languages/Frameworks! </div> <ul> <li class="heading">HTML/CSS/JavaScript</li> <li>Python / Flask</li> <li>Heroku</li> </ul> <!-- a comment node --> <script src="../js/script.js"></script> </body> </html>
css/style.css
.highlight { color: red; background-color: yellow; }
js/script.js
function printBodyDOM() { var childNodes = document.body.childNodes; for(var i=0; i<childNodes.length; i++) { alert(childNodes[i]); } } printBodyDOM();
function printHeadingClassElements() { var childNodes = document.querySelectorAll(".heading"); for(var i=0; i<childNodes.length; i++) { alert(childNodes[i]); } } printHeadingClassElements();
function printTitleIDElements() { var childNodes = document.querySelectorAll("#title"); for(var i=0; i<childNodes.length; i++) { alert(childNodes[i]); } } printTitleIDElements();
function printLIElements() { var childNodes = document.querySelectorAll("li"); for(var i=0; i<childNodes.length; i++) { alert(childNodes[i]); } } printLIElements();
These run significantly faster than querySelectorAll, and should be used when possible
function changeTitleText(titleText) { var titleNode = document.querySelectorAll("#title")[0]; titleNode.innerHTML = titleText; } var titleText = prompt("Change title text"); changeTitleText(titleText);
function addClass(className) { var ulNode = document.querySelectorAll("ul")[0]; ulNode.classList.add(className); } var className = prompt("Add Class"); addClass(className);
Note: to run, may need to run: "[sudo] pip install requests" to install requests package
import requests link = "https://www.ischool.berkeley.edu/people/kay-ashaolu" f = requests.get(link) print(f.text)
On a user event, run this function
var titleNode = document.querySelector("#title"); titleNode.addEventListener("click", function(event) { titleNode.classList.add("highlight"); })
html/index.html (1/2)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" href="../css/style.css" /> </head> <body> <form id="locationForm"> <label for="name">Name: </label> <input type="text" name="name" id="name" /><br /> <label for="state">State</label> <select name="state"> <option value="CA">CA</option> <option value="NY">NY</option> <option value="TX">TX</option> </select><br /> <input type="submit" value="Save Location" /> </form> <br />
html/index.html (2/2)
<div id="locationList"> </div> <script src="../js/script.js"></script> </body> </html>
css/style.css (1/1)
.highlight { color: red; background-color: yellow; }
js/script.js (1/1)
/* Populate with current location */ var locationDiv = document.getElementById("#locationList"); locationDiv.innerHTML = localStorage.curLocation; /* Run code on submit button push */ var locationForm = document.getElementById("#locationForm"); locationForm.addEventListener("submit", function(event) { var name = locationForm.elements.namedItem("name").value; var state = locationForm.elements.namedItem("state").value; localStorage.curLocation = name + ": " + state; var locationDiv = document.getElementById("locationList"); locationDiv.innerHTML = localStorage.curLocation; locationDiv.classList.add("highlight"); /* This stops the usual function of "submit" which is to send data to another server */ event.preventDefault(); })