[JavaScript] Understanding Onclick Event Listener and Various Ways to Code It.
Onclick, addEventListener(), DOM, querySelector(), getElementById()
![[JavaScript] Understanding Onclick Event Listener and Various Ways to Code It.](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1683705052167%2Fb6ac5390-ae96-43b1-a143-f58b5327f163.jpeg&w=3840&q=75)
Computer Science Enthusiast with a Drive for Excellence | Data Science | Web Development | Passionate About Tech & Innovation
Introduction
This article explores two ways of adding an event listener onclick().
Adding onclick="" property in your HTML tag.
Using addEventListener() method in JavaScript.
[JavaScript] Accessing the DOM Using JavaScript - Explained with Examples.
Onclick Property in HTML Tag
Onclick property is written in your HTML tag, and receives the function after the assignment operator(=).
That function is executed when the user clicks a button.
Before going through this article, if you don't know how to access the DOM(document object model), check out this article.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button onclick="doSave();" id="btn">Save</button>
</body>
<script>
function doSave() {
// code in here will be executed when the user clicks a button
}
</script>
</html>
Using addEventListener Method in JavaScript
You can use an addEventListener method to do the same task as above.
I prefer using this, and many developers also prefer to do this because your code is more manageable and readable.
Imagine you have a thousand lines of HTML code. It would be quite hard to manage them. So, we can create a function in JavaScript to make it more manageable.
AddEventListener receives two parameters:
Event type
Function that will be executed when the event happens.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button id="btn">Save</button>
</body>
<script>
function addClickEvent() {
document.querySelector("#btn")
.addEventListener("click", function() {
// code in here will be executed when the user clicks a button
}
}
</script>
</html>
What the function addClickEvent() is doing:
Access the DOM by using the id="btn".
Specifies that the event listener "click" is added.
When a user clicks a button, anything inside the function (second parameter of addEventListener) will be executed.
![[CS fundamentals] Linked List Creation & Insertion explained in C.](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F644c253f17f6efe1e02ade41%2F6a74e5c0-44ca-4110-9ee7-e8e67767a9dc.png&w=3840&q=75)
![[CS Fundamentals] Pointers in C](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F644c253f17f6efe1e02ade41%2F6f81f5a6-d87c-4382-883f-fa4d0a8b5968.png&w=3840&q=75)
![[SQL] Writing Efficient SQL Queries](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1751292603168%2Fb0b43c7d-8e08-47b2-a64c-b600e3511831.jpeg&w=3840&q=75)
![[CS Fundamentals] Binary Search](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1747041878054%2Fc8d8adb9-097d-484b-9562-f43e4bce9a46.png&w=3840&q=75)
![[CS Fundamentals] Sorting Algorithms](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1746973590876%2Ffab37371-1496-42ef-950b-432e7637c89e.png&w=3840&q=75)