Browser Events
Browser Events: Browser Events
What is an event in relation to the DOM, Browser, and JavaScript?
View Answer:
<button id="myButton">Click Me</button>
<script>
// Add event listener to the button
var button = document.getElementById("myButton");
button.addEventListener("click", handleClick);
// Event handler function
function handleClick(event) {
console.log("Button clicked!");
// Additional code logic here...
}
</script>
In this example, we have an HTML button with the id "myButton". We use JavaScript to get a reference to the button using document.getElementById()
. Then, we add an event listener to the button using addEventListener()
. When the button is clicked, the handleClick
function is called, which logs a message to the console. You can add additional code logic inside the event handler function to perform actions in response to the event.
Can you name at least three DOM events?
View Answer:
What is a JavaScript event handler?
View Answer:
<button id="myButton">Click Me</button>
<script>
// Event handler function
function handleClick(event) {
console.log("Button clicked!");
// Additional code logic here...
}
// Add event listener to the button
var button = document.getElementById("myButton");
button.addEventListener("click", handleClick);
</script>
In this example, we have a button with the id "myButton". The handleClick
function is an event handler that is called when the button is clicked. It logs a message to the console. We then add an event listener to the button using addEventListener()
, specifying the "click" event and the handleClick
function as the event handler.
Can you execute an event in an HTML attribute?
View Answer:
<!-- alerts Click! in the browser -->
<input value="Click me" onclick="alert('Click!')" type="button">
<script>
function countRabbits() {
for (let i = 1; i <= 3; i++) {
alert('Rabbit number ' + i);
}
}
</script>
<input type="button" onclick="countRabbits()" value="Count rabbits!" />
Can you connect a handler with a DOM element "on<event>" property?
View Answer:
<input id="elem" type="button" value="Click me" />
<script>
elem.onclick = function () {
alert('Thank you');
};
</script>
Is it possible to utilize an event on a DOM element more than once?
View Answer:
<input type="button" id="elem" onclick="alert('Before')" value="Click me" />
<script>
elem.onclick = function () {
// overwrites the existing handler
alert('After'); // only this After will be shown
};
</script>
What is the reason for not using setAttribute for handlers?
View Answer:
// a click on <body> will generate errors,
// because attributes are always strings, function becomes a string
document.body.setAttribute('onclick', function () {
alert(1);
});
Are DOM properties names case-sensitive or case-insensitive?
View Answer:
var element = document.createElement('div');
element.innerHTML = 'Hello, JavaScript!';
console.log(element.innerHTML); // Output: Hello, JavaScript!
console.log(element.innerhtml); // Output: undefined
Can you explain the function of the EventTarget addEventListener method?
View Answer:
target.addEventListener(event, handler [, options]);
target.addEventListener(event, handler [, useCapture]);
target.addEventListener(event, handler [, useCapture, wantsUntrusted
// This API has not been standardized.
]); // Gecko/Mozilla only
<input id="elem" type="button" value="Click me" />
<script>
function handler1() {
alert('Thanks!');
}
function handler2() {
alert('Thanks again!');
}
elem.onclick = () => alert('Hello');
elem.addEventListener('click', handler1); // Thanks!
elem.addEventListener('click', handler2); // Thanks again!
</script>
Can we remove an event listener?
View Answer:
<script>
function handler() {
alert('Thanks!');
}
input.addEventListener('click', handler);
// ....
input.removeEventListener('click', handler);
</script>
Are there any events that you cannot assign using a DOM property?
View Answer:
// will never run
document.onDOMContentLoaded = function() {
alert("DOM built");
};
// this way it works
document.addEventListener("DOMContentLoaded", function() {
alert("DOM built");
});
What is the event object used for in JavaScript?
View Answer:
<!-- event.type returns the type of event (onclick: click) -->
<input type="button" onclick="alert(event.type)" value="Event type" />
Are we limited to just function handlers in JavaScript?
View Answer:
<button id="elem">Click me</button>
<script>
let obj = {
handleEvent(event) {
alert(event.type + ' at ' + event.currentTarget);
},
};
elem.addEventListener('click', obj);
</script>
<button id="elem">Click me</button>
<script>
class Menu {
handleEvent(event) {
switch (event.type) {
case 'mousedown':
elem.innerHTML = 'Mouse button pressed';
break;
case 'mouseup':
elem.innerHTML += '...and released.';
break;
}
}
}
let menu = new Menu();
elem.addEventListener('mousedown', menu);
elem.addEventListener('mouseup', menu);
</script>
What's the difference between "event.target" and "event.currentTarget"?
View Answer:
<!DOCTYPE html>
<html>
<head>
<title>Event Target vs CurrentTarget</title>
</head>
<body>
<div id="outer">
<div id="inner">
Click me!
</div>
</div>
<script>
function handleClick(event) {
console.log('Target:', event.target.id);
console.log('CurrentTarget:', event.currentTarget.id);
}
var outer = document.getElementById('outer');
var inner = document.getElementById('inner');
outer.addEventListener('click', handleClick);
inner.addEventListener('click', handleClick);
</script>
</body>
</html>
Output:
Target: inner
CurrentTarget: outer
In the example, we have an outer <div>
with an id of "outer" and an inner <div>
with an id of "inner". Both elements have a click event listener attached to them.
When you click on the inner <div>
, the event bubbles up to the outer <div>
. The event handler handleClick
is called, and within the function, event.target
refers to the element that triggered the event (in this case, the inner <div>
with the id "inner"). On the other hand, event.currentTarget
refers to the element that currently has the event listener attached to it (in this case, the outer <div>
with the id "outer").
Running the code and inspecting the console will display the following output when clicking on the inner <div>
:
This demonstrates the difference between event.target
and event.currentTarget
.