CSCI 111 lecture 5
CSCI 111 lecture 5
Solving
All of these actions are called events and can be handled by the website
Inline events
Each time a user interacts with the web page, the browser triggers one of
the predefined events such as:
● Mouse events
● Keyboard events
● Form events
● Window events
To handle the event, we can directly write event handler in the HTML
element’s opening tag
Inline events
Each type of events has a predefined name to handle that event. For
example for the mouse events:
● mouseover
● mouseenter
● mouseout
● etc.
let h = document.getElementById("title")
h.addEventListener(
"click", Event type
function(event) Event handler, Note the event argument of the function – event object
{
this.style.color="red";
});
h.addEventListener(
"click",
function (event)
{
console.log(event.clientX, event.clientY)
}
); from Example: 3.html (third h1)
Useful links
● https://www.w3schools.com/js/js_events.asp
● https://www.w3schools.com/jsref/dom_obj_event.asp
● https://www.w3schools.com/js/js_htmldom_events.asp
● https://www.w3schools.com/js/js_htmldom_eventlistener.asp
● https://www.w3schools.com/js/js_events_examples.asp
● https://www.w3schools.com/js/js_this.asp
Summary
● JavaScript can be used to handle events caused by user interaction
with the web page: mouse, keyboard, browser, form, window events
● There are two ways to handle the events on the webpage:
○ Inline events
○ Event listeners
● this keyword can be used to access the element where event
happened
● To get more information about the event, we can use event object
provided by the event listeners