← Back to JavaScript Course | Chapter 2: Functions & Objects | Lesson 6 of 10

JS Events

Think of a doorbell, it sits quietly doing nothing until someone presses it, and only then does the chime inside the house react. The button doesn't run on a schedule, it waits for a specific trigger. JavaScript events work the same way, they let your code wait quietly until something specific happens, a click, a mouse movement, a typed character, and only then run in response. This event-driven approach is what makes pages feel alive, and it's exactly how interactive features across cookiescursor.com, from buttons to menus, actually work under the hood.
Syntax
javascript
element.addEventListener("eventName", function(event) {
  // handler code
});

<element onclick="handler()">

onclick

onclick तब चलता है जब किसी element पर click किया जाए, और इसे या तो inline HTML attribute के रूप में सेट किया जा सकता है या सीधे JavaScript में assign किया जा सकता है ताकि कोई विशिष्ट function चले।

Note: onclick लगभग हर दिखने वाले element पर काम करता है, केवल buttons पर नहीं, links, images और divs भी clicks पर प्रतिक्रिया दे सकते हैं।
Warning: Setting element.onclick a second time overwrites the first handler entirely, only one function can be assigned this way at a time.

उदाहरण: onclick

javascript
<button onclick="console.log('Clicked!')">Click Me</button>

onmouseover

onmouseover तब चलता है जब mouse pointer किसी element की सीमाओं में प्रवेश करता है, और आमतौर पर onmouseout के साथ जोड़ा जाता है, जो pointer के बाहर जाने पर चलता है, ताकि hover-आधारित प्रभाव बनाए जा सकें।

Note: सरल दृश्य hover प्रभावों के लिए, CSS का :hover अक्सर JavaScript से सरल होता है, onmouseover को उन प्रभावों के लिए बचाकर रखिए जिन्हें असली logic चाहिए।
Warning: onmouseover fires again every time the cursor re-enters a child element inside the same parent, which can trigger it more often than expected.

उदाहरण: onmouseover

javascript
<div onmouseover="console.log('Mouse entered')" onmouseout="console.log('Mouse left')">Hover over me</div>

onchange

onchange तब चलता है जब किसी form element का value बदला गया हो और फिर वह focus खो दे, जो आमतौर पर dropdowns, checkboxes और text inputs के साथ user के चुनावों पर प्रतिक्रिया देने में इस्तेमाल होता है।

Note: text inputs के लिए, onchange केवल field के focus खोने के बाद चलता है, अगर आपको हर keystroke पर प्रतिक्रिया देनी हो तो इसके बजाय input event का उपयोग कीजिए।
Warning: onchange does not fire while the user is actively typing in a text field, only after they click or tab away from it.

उदाहरण: onchange

javascript
<!-- The onchange attribute below runs this JS whenever the selection changes -->
<select onchange="console.log('Selection changed to', this.value)">
  <option value="a">A</option>
  <option value="b">B</option>
</select>

addEventListener

addEventListener किसी element से event handler को मौजूदा handlers को अधिलेखित किए बिना जोड़ता है, और एक ही event के लिए कई listeners जोड़ने देता है, जो इसे events संभालने का आधुनिक, अनुशंसित तरीका बनाता है।

Note: addEventListener का एक मिलता-जुलता removeEventListener भी है, जो उन handlers को साफ़ करने में उपयोगी है जिनकी अब ज़रूरत नहीं, जैसे जब कोई element हटा दिया जाए।
Warning: removeEventListener requires the exact same function reference used in addEventListener, an anonymous function cannot be removed this way.

उदाहरण: addEventListener

javascript
<button>Click Me</button>
<script>
  // Declare the constant `btn`, set to `document.querySelector("button")`
  const btn = document.querySelector("button");
  // Listen for the `click` event on `btn` and run the handler when it fires
  btn.addEventListener("click", () => console.log("First listener"));
  // Listen for the `click` event on `btn` and run the handler when it fires
  btn.addEventListener("click", () => console.log("Second listener"));
</script>

Event Object

हर event handler को अपने पहले argument के रूप में अपने-आप एक event object मिलता है, जिसमें यह ब्योरा होता है कि क्या हुआ, जैसे किस element ने इसे शुरू किया, mouse निर्देशांक, या कौन सी key दबाई गई।

Note: event.target सबसे उपयोगी properties में से एक है, यह हमेशा उसी सटीक element को दर्शाता है जिस पर event असल में हुआ।
Warning: event.target and this can refer to different elements when using event delegation, understanding the difference avoids confusing bugs.

उदाहरण: The Event Object

javascript
<button onclick="console.log(event.target)">Click Me</button>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.