August 2023 to June 2024
View the Project on GitHub IshanCornick/new_student
Home | HTML | JavaScript | DOM | Data Types | JS Debugging |
Remember to “git pull” on teacher repository to update to lates.
document.getElementById("idTag")
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>
%%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>
%%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>
%%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>
%%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>
%%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>
%%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>
%%html
<!-- HTML Code -->
<html lang="en">
<head>
<!-- ... (your existing head content) ... -->
</head>
<body>
<h1>Enhanced HTML Example</h1>
<!-- ... (your existing content) ... -->
<!-- Swap Links Button -->
<button onclick="swapLinks()">Swap Links</button>
<!-- Top Paragraph to Change -->
<p id="topParagraph">Original Text</p>
<!-- Links to be Swapped -->
<a id="Example" href="https://example.com" onclick="updateTopParagraph('Example')">Example</a>
<a id="Openai" href="https://openai.com" onclick="updateTopParagraph('Openai')">Openai</a>
<script>
function showHiddenMessage() {
const hiddenMessage = document.querySelector('.hidden-message');
hiddenMessage.style.display = 'block';
}
function swapLinks() {
const Example = document.getElementById('Example');
const Openai = document.getElementById('Openai');
// Swap the href attributes of the two links
const tempHref = Example.href;
Example.href = Openai.href;
Openai.href = tempHref;
// Swap the inner HTML (text) of the two links
const tempText = Example.innerHTML;
Example.innerHTML = Openai.innerHTML;
Openai.innerHTML = tempText;
// Change the inner HTML of the top <p> tag
const topParagraph = document.getElementById('topParagraph');
topParagraph.innerHTML = 'Switched!';
}
function updateTopParagraph(linkId) {
const topParagraph = document.getElementById('topParagraph');
topParagraph.innerHTML = `Link ${linkId} pressed!`;
}
</script>
</body>
</html>
Original Text
Example OpenaiHome | HTML | JavaScript | DOM | Data Types | JS Debugging |
Remember to “git pull” on teacher repository to update to lates.
document.getElementById("idTag")
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>
%%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>
%%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>
%%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>
%%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>
%%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>
%%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>
%%html
<!-- HTML Code -->
<html lang="en">
<head>
<!-- ... (your existing head content) ... -->
</head>
<body>
<h1>Enhanced HTML Example</h1>
<!-- ... (your existing content) ... -->
<!-- Swap Links Button -->
<button onclick="swapLinks()">Swap Links</button>
<!-- Top Paragraph to Change -->
<p id="topParagraph">Original Text</p>
<!-- Links to be Swapped -->
<a id="Example" href="https://example.com" onclick="updateTopParagraph('Example')">Example</a>
<a id="Openai" href="https://openai.com" onclick="updateTopParagraph('Openai')">Openai</a>
<script>
function showHiddenMessage() {
const hiddenMessage = document.querySelector('.hidden-message');
hiddenMessage.style.display = 'block';
}
function swapLinks() {
const Example = document.getElementById('Example');
const Openai = document.getElementById('Openai');
// Swap the href attributes of the two links
const tempHref = Example.href;
Example.href = Openai.href;
Openai.href = tempHref;
// Swap the inner HTML (text) of the two links
const tempText = Example.innerHTML;
Example.innerHTML = Openai.innerHTML;
Openai.innerHTML = tempText;
// Change the inner HTML of the top <p> tag
const topParagraph = document.getElementById('topParagraph');
topParagraph.innerHTML = 'Switched!';
}
function updateTopParagraph(linkId) {
const topParagraph = document.getElementById('topParagraph');
topParagraph.innerHTML = `Link ${linkId} pressed!`;
}
</script>
</body>
</html>
Original Text
Example Openai