diff --git a/client/src/guide/english/javascript/event-handling/index.md b/client/src/guide/english/javascript/event-handling/index.md new file mode 100644 index 0000000000..b57bc9c9d9 --- /dev/null +++ b/client/src/guide/english/javascript/event-handling/index.md @@ -0,0 +1,137 @@ +--- +title: Event Handling +--- + +## Event Handling + +Events are (in the context of JavaScript) actions or incidents which happen inside the browser window, such as + +* the user clicks on something or hovers over it +* the user presses a key +* the user drags and drops something +* the user submits a form +* content finishes loading +* a video starts or stops playing +* an error occurrs + +and many more. + +When an event happens it sends a signal which your code can react to. + +Events usually occur on a specific item in the browser window, such as an element, a group of elements, the HTML document in your current tab, or the browser window itself. + +You can use JavaScript to attach an event handler (also called an event listener) to these items to "listen" for a particular event and run some code when the event happens. + +Inside the event handler, you specify two things: the type of event you are listening for, and the code you want to run when the event occurs. + +```html + +``` + +```js +const button = document.querySelector("button"); + +button.addEventListener("click", () => console.log("Clicked!")); +``` + +Here you are listening for a click on the button, and when it happens you are logging "Clicked!" to the browser console. + +### Avoid inline event handlers + +The earliest way of handling events was inline event handlers where you write your JavaScript as the element's attribute value. + +```html + +``` + +This can become unmaintainable and inefficient. Just like with inline styles in CSS, if you change your mind later you have to go through your HTML and change every inline event handler one by one; and you might miss one. It's better to select elements and add event listeners in your seperate JavaScript file, where you can select multiple elements at the same time. + +### The Event Object + +Whenever you interact with the browser window, you create an event object. The event object has properties, such as the type of event (`click`, or `mousedown`), the target (`div` or `button`), and others. + +The event object is often passed as a parameter (by convention called `event`, `evt` or `e`) to event handler functions, for example to help the function to target the desired element. In the following example, `e.target` points to the button that you clicked on. + +```js +const button = document.querySelector("button"); + +button.addEventListener("click", changeButtonColor; + +function changeButtonColor(e) { + const randomColor = `rgb(${random(255)}, ${random(255)}, ${random(255)})`; + e.target.style.backgroundColor = randomColor; +} +``` + +### Event Bubbling + +Any time you click on an element, you are really also clicking on its parent elements because they surround it and so it is part of them. When you click on an element, the browser checks if it has an event handler attached to it and runs the handler. It then moves to the parent element and checks if it has an event handler, and repeats this until it reaches the `` element. The event "bubbles" up through the DOM. + +In the example below, if you click on div three it will log `three, two, one` to the console as the click event bubbles up and triggers the event handler on each div. + +```html +