JavaScript/C3/Events-and-Event-Listeners-in-JS/English
Title of the script:Events and Event Listeners
Author: Jayesh Katta Ramalingaiah
Domain Reviewer: Ankita Maske
Novice Reviewer: Praveeen S.
Keywords: JavaScript, HTML, events, event listeners
| |
|
| Slide: Title | Hello and Welcome to the spoken tutorial on “Events and Event Listeners in JS”. |
| Slide:
Learning Objectives |
In this tutorial, we will learn about:
|
| Slide: System Specifications | This tutorial is recorded using:
However you may use any other browser of your choice. |
| Slide : Pre-requisites | To practice this tutorial,
|
| Slide: Code files |
|
| Slide: Events |
|
| Slide: Events |
|
| Slide: Adding & Removing Event Listeners
|
|
| Only Narration | Now let us take an example and understand events better. |
| Show VS editor | Open Visual Studio Code editor. |
| [Editor]
|
In the editor, browse and open the folder “Practice-JS ”.
|
| [Editor] Click on Explorer pane -> Practice-JS -> index.html | Under Practice-JS folder, open the file named index.html
|
| [Editor] Type:
<html lang="en"> <head> <title> Events and Event Listeners </title> </head> <body> <button id = "clickButton" > CLICK ME! </button> <script src = "main.js" > </script> </body> </html> |
In the index.html file, update the code as shown.
|
| [Editor] Highlight:
|
Here, I have created a button with id clickButton. |
| Press: Ctrl + S | Save the file. |
| Press: Alt + L and Alt + O | Start the Live server. |
| Show firefox | The default browser will open automatically and a new tab opens. |
| [Firefox] Press Ctrl + Shift + I
|
Now open the Browser developer tools panel and go to the console tab. |
| Switch to Editor | Switch back to the editor. |
| [Editor] Click on
|
Under Practice-JS folder, open the file named main.js |
| [Editor] Type:
console.log("You clicked on the Button!"); }
|
In the main.js file, update the code as shown. |
| [Editor] [Highlight]:
console.log("You clicked on the Button!"); } |
Here, I have just declared a normal function, which logs - “You clicked on the Button!” |
| [Editor] [Highlight]:
|
You are already aware that getElementById function returns an element with a specified ID. |
| [Editor] [Highlight]:
|
For the returned element, I’m adding an event listener, using the addEventListener method. |
| [Editor] [Highlight]:
|
addEventListener method accepts two mandatory parameters.
|
| Press: Ctrl + S | Save the file. |
| Switch to browser | Switch back to the browser. |
| [Browser]
Click on Button [CLICK ME!] |
Click on the Click Me button in the webpage. |
| [Browser] [Console Tab]:
You clicked on the Button! |
We can see a log printed in the console on clicking the button.
|
| Switch to Editor | Switch back to the editor. |
| [Editor] Type:
|
Add this line in the main.js file after the last line. |
| [Editor] [Highlight]:
|
You are already aware that getElementById function returns an element with specified ID. |
| [Editor] [Highlight]:
|
For the returned element, I’m removing the event listener using the removeEventListener method. |
| [Editor] [Highlight]:
|
removeEventListener method accepts two mandatory parameters.
|
| Press: Ctrl + S | Save the file. |
| Switch to Browser | Switch back to the browser. |
| [Browser] [Console Tab]:
|
We cannot see any log printed in the console on clicking the button.
|
| Only narration | With this we have come to the end of this tutorial.
|
| Slide: Summary | In this tutorial, we have learnt:
|
| Slide: Assignment | As an assignment,
|
| Slide: Assignment |
|
| Slide: Assignment |
|
| Slide: About Spoken Tutorial Project |
|
| Slide: Spoken tutorial workshops |
|
| Slide: Forum questions | Pls post your timed queries in this forum. |
| Slide: Acknowledgement | Spoken Tutorial Project is funded by Ministry of Education (MoE), Government of India |
| Slide: Thanks | The script for this tutorial is contributed by Jayesh.
And this is Praveen from IIT Bombay signing off. Thank you for joining. |