Home | HTML | Data Types | DOM | JavaScript | JS Debugging |
Following along
Remember to “git pull” on teacher repository to update to lates.
- Run this notebook in VSCode
- Activate Help-Toogel Developer Tools to add console outputs to runtime experience
Referencing HTML elements using javascript
- To get an HTML element, use
document.getElementById("idTag")
- You will use the ID that you set in your HTML
- if you
console.log
the resulting variable you will get some information about the element
%%html
<!-- the ID must be specified within the element -->
<h1 id="domTitleID">My Title</h1>
<!-- javascript goes here -->
<script>
var titleElement = document.getElementById("domTitleID")
<!-- outputs h1 tag -->
console.log("Example #1, show element in DOM")
console.log(titleElement)
</script>
My Title
Getting the data within the HTML element
- The variable titleElement stores the “object”
- Basically think of this as the group of data enclosed in HTML tag
- To access a certain type of data from an “object” we use “.” notation
- .innerHTML gets data within center of tag
%%html
<!-- the ID must be specified within the element -->
<h1 id="domTitleIDget">My Title</h1>
<!-- javascript goes here -->
<script>
var titleElement = document.getElementById("domTitleIDget")
<!-- outputs h1 innerHTML from h1 tag -->
console.log("Example #2, show innerHTML")
console.log(titleElement.innerHTML)
</script>
My Title
Setting the data within the HTML Element
- The innerHTML data in this “object” can be set like a variable
- Change the value of the innerHTML using the “=” (assignment) operator
%%html
<!-- the ID must be specified on the element -->
<h1 id="domTitleIDset">My Title</h1>
<!-- javascript goes here -->
<script>
var titleElement = document.getElementById("domTitleIDset")
titleElement.innerHTML = "Set and Update My Title"
<!-- outputs h1 innerHTML after h1 tag has been updated -->
console.log("Example #3, update innerHTML")
console.log(titleElement.innerHTML)
</script>
Creating elements
- Create a new element with the document.createElement function -> takes in the type of element
- Set properties in the element just like the “h1” example
%%html
<!-- the ID must be specified on the element -->
<div id="divContainerID">
<h1 id="h1ElementID">My Title</h1>
</div>
<!-- javascript goes here -->
<script>
// creates a new element
var pElement = document.createElement("p")
pElement.innerHTML = "Starting a paragraph of text."
// outputs p tag after it has been created
console.log("Example #4, create a p tag within JS")
console.log(pElement)
</script>
Issue! How to Create element that appears in HTML?
- Here is a visualization of what is happening => the “p” is not placed inside the HRML page!
Solution
- Correct by placeing the element somewhere in the page
- For example, we could add the element within the div
- For this, use the appendChild function on the div object (the parameter would be the p element we created)
- Remember, use the getELementById to get the object for something in the html (the div!)
- Updated Diagram
%%html
<!-- the ID must be specified on the element -->
<div id="divContainerIDset">
<h1 id="h1ElementIDset">My Title</h1>
</div>
<!-- javascript goes here -->
<script>
// creates a new element
var pElement = document.createElement("p")
pElement.innerHTML = "Starting a paragraph of text."
// outputs p tag after it has been created
console.log("Example #5, add p tag to HTML")
console.log(pElement)
// place the p element inside the HTML page
var div = document.getElementById("divContainerIDset")
div.appendChild(pElement)
</script>
Functions in JavaScript, using with DOM
- Functions allow you to “do something”
- ex. “eat food” in a Snake Game
- Functions were used in previous examples
- console.log = “print something”
- document.getElementById = “find an element with id”
- Functions take in parameters, what to do (inside the parenthesis)
- the parameter tells console.log what to print
- the parameter in document.getElementById tells the id of the element
- Functions can be used with DOM as well, thes will be shown below
Creeating functions
- document functions functions were used to create a lot of functionality, but how can a developer create their own?
- function are useful to avoid writing the same code over and over again
- function can contain parameters for input (they effectively become variables)
- function can contain a return, the are the “output” of the function
%%html
<!-- the ID must be specified on the element -->
<div id="divContainerIDfunction">
<h1 id="h1ElementIDfunction">My Title</h1>
</div>
<!-- javascript goew here -->
<script>
// define a function => takes parameter text, returns a new p tab
function createPTag(text) {
// creates a new element
var pElement = document.createElement("p")
// using the parameter like a variable
pElement.innerHTML = text
// outputs p tag after it has been created
console.log("Example #6, add p tag using a function")
console.log(pElement)
return pElement;
}
// using a function to create p tag
var pTag = createPTag("Starting a paragraph with cooler text than before.")
// place the p element in the webpage
var div = document.getElementById("divContainerIDfunction")
div.appendChild(pTag)
</script>
OnClick Event
- Run a function when an event occurs
- In this case, the p tag is created when the button is clicked
%%html
<!-- the ID must be specified on the elements -->
<button id="buttonID">Click here!</button>
<div id="divContainerIDbutton">
<h1 id="h1ElementIDbutton">My Title</h1>
</div>
<!-- our javascript goe here -->
<script>
// define a function => takes parameter text, returns a new p tab
function createPTag(text) {
// creates a new element
var pElement = document.createElement("p")
// using the parameter like a variable
pElement.innerHTML = text
// outputs p tag after it has been created
console.log("Example #7.1, add p tag using a function")
console.log(pElement)
return pElement;
}
// create a function that sets specific text and adds to div
function addPTagOnButton() {
// using our new function
var pTag = createPTag("Starting a paragraph with text created on button press.")
// place the p element in the webpage
var div = document.getElementById("divContainerIDbutton")
// add p tag to the div
div.appendChild(pTag)
// outputs p tag after it has been created
console.log("Example #7.2, update container adding a 'p' tag")
console.log(div)
}
// add the P tag when our button is clicked
var myButton = document.getElementById("buttonID")
myButton.onclick = addPTagOnButton
</script>
Hacks
- Copy your HTML code from the HTML hacks. Write a Javascript snippet to switch the links of the two a tags when a button is pressed. Once they are switched, change the inner HTML of the top p tag to the word “switched!”
%%html
<head>
<title>HTML Hacks</title>
<style>
.box {
width: 525;
padding: 15;
margin: 10;
background-color: rgb(197, 197, 171);
}
.text {
font-size: 13;
color: black;
}
button {
width: 200;
height: 75;
font-size: 28;
background-color: white;
animation: rgb 3 linear infinite alternate;
}
@keyframes rgb {
0% {
background-color: red;
}
20% {
background-color: yellow;
}
40% {
background-color: green;
}
60% {
background-color: blue;
}
80% {
background-color: blueviolet;
}
100% {
background-color: red;
}
}
.box2 {
width: 525;
padding: 10;
margin: 10;
background-color: rgb(197, 197, 171);
}
.text2 {
font-size: 13;
color: black;
}
</style>
</head>
<body>
<h1><strong>Basics of HTML hacks</strong></h1>
<div class="box">
<p class="text">The thing that I am most passionate about is probably going to be Soccer. My passion for soccer is not just liking the sport, but also the connections that I have among my teammates and my friends. Soccer made me and my teammates and friends bonded together, feeling like a big family.</p>
</div>
<br>
<!-- This is the button that goes to the passion -->
<div>
<div>
<a href="https://www.sdfacademy.com/" id="link1"><button><strong>CLICK HERE</strong></button></a>
</div>
</div>
<br>
<!-- Two links that are related to my passions. -->
<div>
<a href="https://www.premierleague.com/" id="link2">Premier League</a>
<br>
<a href="https://www.uefa.com/uefachampionsleague/" id="link3">UEFAchampionsleague</a>
</div>
<!-- Button to switch links and change paragraph text -->
<button id="switchButton">Switch Links</button>
<script>
// Function to switch the links and change the top paragraph text
function switchLinksAndText() {
// Get references to the two <a> tags
var link1 = document.getElementById("link3");
var link2 = document.getElementById("link2");
var text1 = link1.textContent
var text2 = link2.textContent
// Store their href attributes
var href1 = link1.href;
var href2 = link2.href;
// Swap the href attributes
link1.href = href2;
link2.href = href1;
link1.textContent = text2;
link2.textContent = text1;
// Change the top paragraph text to "switched!"
var topParagraph = document.querySelector(".text");
topParagraph.innerHTML = "switched!";
}
// Get a reference to the switch button
var switchButton = document.getElementById("switchButton");
// Add a click event listener to the button
switchButton.addEventListener("click", switchLinksAndText);
</script>
</body>
Basics of HTML hacks
The thing that I am most passionate about is probably going to be Soccer. My passion for soccer is not just liking the sport, but also the connections that I have among my teammates and my friends. Soccer made me and my teammates and friends bonded together, feeling like a big family.