- Recall the different types of JavaScript Events
- Identify when the
DOMContentLoaded
event will trigger - Implement a callback attached to an event handler
Take a look at each of the code samples below. For each sample, work with your group to answer the following questions.
- What does each line of code do?
- How does this piece of code work?
- Given this code sample, what can you learn or describe about JavaScript
// index.html
...
<button id="notify">Click Me!</button>
<script src="index.js" />
// index.js
const button = document.getElementById("notify")
button.addEventListener('click', function(){
console.log("Printing a Message!")
})
// index.html
...
<button id="notify">Click Me!</button>
<script src="index.js" />
// index.js
const button = document.getElementById("notify")
button.addEventListener('mouseover', function(){
console.log("Printing a Message!")
})
// index.html
...
<script src="index.js" />
<button id="notify">Click Me!</button>
// index.js
const button = document.getElementById("notify")
button.addEventListener('mouseover', function(){
console.log("Printing a Message!")
})
// index.html
...
<script src="index.js" />
<button id="notify">Click Me!</button>
// index.js
document.addEventListener("DOMContentLoaded", function(){
const button = document.getElementById("notify")
button.addEventListener('mouseover', function(){
console.log("Printing a Message!")
})
});
Oops. Looks like this developer made some mistakes typing. Identify the mistakes.
// index.html
...
<script src="index.js" />
<button id="notifiable">Click Me!</button>
// index.js
document.addListenerEvent("DOMContentLoaded", function(){
const button = document.getElementById("notifliable")
button.addListenerEvent('click', function(){
console.log("Printing a Message!")
})
});
Maryam Salah, Mhamad Marshall, Wafa'a Al-Hayek, Yousif Ismail
example 1:
1- the first the line print the button with the id notify
2- we have a button with the id of notify and we have script that target the button with id of notify , then we assign it a variable called button, then we add an event listener to variable with the function that each time you click the button it print in the console("printing a message");
3-with js we can add behavior to the HTML tags.
Example 2:
1- the first the line print the button with the id notify
2- we have a button with the id of notify and we have a script that targets the button with id of notify , then we assign it a variable called button, then we add an event listener to variable with the function that each time you hover the button it print in the console("printing a message");
3-with js we can add behavior to the HTML tags.
Example 3:
1-by writing js script above the HTML tag, there is a possibility that might not work.
2- This script would work if we add it below the HTML tag.
3-by doing this the hover effect would work.
Example 4:
1- It is the same as the example above except when the content is loaded it will go directly to the function.
Example 5:
1- It's addEventListener not addListenerEvent, ( notifliable) is not correct with the spelling of the original id of the button.