Hello friends! Almost every interactive web page starts with one simple thing: something happens when you click. The click event fires when a user clicks an element: a button, a link, an image, almost anything on the page. It is the most common way to make a web page respond to the user. There are three ways to run JavaScript on a click, and this lesson shows all three, with the one you should normally use. Let’s begin with the oldest and simplest way.
1. The onclick HTML Attribute
<button onclick="sayHello()">Click me</button>
<p id="msg"></p>
<script>
function sayHello() {
document.getElementById("msg").textContent = "Hello, Riya!";
}
</script>
Result: when you click the button, the paragraph shows Hello, Riya!
This is quick for small demos, but it mixes JavaScript into your HTML, which becomes hard to maintain in bigger pages.
There is a cleaner way that keeps JavaScript out of your HTML.
2. The onclick Property in JavaScript
<button id="btn">Click me</button>
<script>
const btn = document.getElementById("btn");
btn.onclick = function () {
alert("Button clicked!");
};
</script>
This keeps HTML and JavaScript separate. The limitation: an element can have only one onclick property. Setting it again replaces the earlier function.
And now the method that professional developers use most of the time.
3. addEventListener() (Recommended)
<button id="likeBtn">👍 Like</button>
<span id="count">0</span>
<script>
const likeBtn = document.getElementById("likeBtn");
const countEl = document.getElementById("count");
let likes = 0;
likeBtn.addEventListener("click", () => {
likes++;
countEl.textContent = likes;
});
</script>
Result: each click increases the number next to the button by one.
addEventListener() lets you attach several handlers to the same element, remove them later with removeEventListener(), and control advanced options. Note the event name is "click", without “on”.
Using the Event Object
Here is something many beginners don’t know. The handler receives an event object with details about the click:
document.addEventListener("click", (event) => {
console.log("You clicked:", event.target.tagName);
console.log("Position:", event.clientX, event.clientY);
});
event.target is the exact element that was clicked, and clientX/clientY give the mouse position.
Practical Example: Show and Hide an Answer
<button id="toggle">Show answer</button>
<p id="answer" hidden>The capital of India is New Delhi.</p>
<script>
const toggle = document.getElementById("toggle");
const answer = document.getElementById("answer");
toggle.addEventListener("click", () => {
answer.hidden = !answer.hidden;
toggle.textContent = answer.hidden ? "Show answer" : "Hide answer";
});
</script>
This pattern is used for FAQs, quizzes, menus and “read more” buttons.
Stopping the Default Action
Clicking a link normally opens it, and clicking a submit button sends a form. Call event.preventDefault() to stop that and handle it yourself:
document.getElementById("myLink").addEventListener("click", (event) => {
event.preventDefault();
console.log("Link click handled by JavaScript instead.");
});
Common Mistakes
Before you start using click events in your own projects, watch out for these mistakes:
- Calling the function instead of passing it:
btn.onclick = sayHello();runssayHelloimmediately. Writebtn.onclick = sayHello;(no brackets). - Running the script before the button exists: if your
<script>is in the<head>,getElementByIdreturnsnull. Put the script at the end of the body, or use<script defer>. - Writing “onclick” in addEventListener: it must be
"click". - Making clickable divs: for accessibility, use a real
<button>for actions. It works with the keyboard and screen readers automatically.
So friends, remember the key difference: onclick allows only one handler per element and is overwritten if you set it again, while addEventListener allows many handlers and can remove them later. If you need to pass a value to your function, wrap the call in an arrow function, like btn.addEventListener("click", () => greet("Riya"));. And event.target is simply the element that was actually clicked. Try the show-and-hide example on your own page, and you will have your first interactive feature ready.
More events: Events in JavaScript · ondblclick · onmouseover · Arrow functions