HTML / CSS / JavaScript Tutorial

JavaScript Tutorial

5. Event handling

[this page | pdf | back links]

A responsive website needs to respond to users when the users act in specific ways, e.g. loading a page, clicking on a button, moving a mouse around a document window etc. JavaScript, like many other modern more sophisticated general-purpose programming languages, includes the concept of events. These assign specific functions to specific events, with the functions being invoked if/when the event occurs.


Event handling linked to individual elements, such as what happens when someone clicks on an element, is often implemented by assigning a specific function to the event attribute of that element, see here.


Global events (not linked to specific HTML elements), such as those triggered by loading the page, are typically implemented by using e.g. the document.addEventListener method, e.g.:


               document.addEventListener('load', addtext());



Contents | Prev | Next | JavaScript

Desktop view | Switch to Mobile