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

JS इवेंट्स

एक doorbell के बारे में सोचें, यह चुपचाप कुछ नहीं करता रहता जब तक कोई उसे दबाए, और तभी घर के अंदर की chime react करती है। बटन किसी schedule पर नहीं चलता, यह एक खास trigger का इंतज़ार करता है। JavaScript events भी बिल्कुल वैसे ही काम करते हैं, ये आपके code को कुछ खास होने तक चुपचाप इंतज़ार करने देते हैं, एक click, एक mouse movement, एक टाइप किया गया character, और तभी response में चलते हैं। यही event-driven approach pages को जीवंत महसूस कराता है, और यही ठीक-ठीक वह तरीका है जिससे cookiescursor.com पर buttons से लेकर menus तक के interactive features अंदर से असल में काम करते हैं।
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. #}
आम गलतियां
  1. complex applications के लिए सिर्फ onclick जैसे inline event attributes पर निर्भर रहना, addEventListener कहीं बेहतर scale करता है और कई handlers allow करता है।
  2. यह भूल जाना कि किसी handler को pass किया गया event object उपयोगी details रखता है, जैसे कौन सी key दबाई गई या कौन सा element क्लिक हुआ।
  3. event bubbling को न समझना, जो किसी child element पर click होने पर parent element के click handler को अनपेक्षित रूप से fire करा सकता है।
चैप्टर सारांश
  • Events JavaScript को clicks, mouse movement, और form changes जैसे user actions पर respond करने देते हैं।
  • addEventListener event handlers attach करने का modern, flexible तरीका है, हर element पर कई listeners को support करते हुए।
  • Events डिफ़ॉल्ट रूप से parent elements के ज़रिए ऊपर की ओर bubble होते हैं, यह एक behavior है जिसे complex interactions बनाने से पहले समझना ज़रूरी है।
ब्राउज़र सपोर्ट

Standard DOM events और addEventListener हर modern browser में supported हैं, जो लगभग सभी interactive web pages की foundation बनाते हैं।

🔒

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.