← Back to jQuery Course | Chapter 3: Events | Lesson 7 of 11

jQuery Event Object

event object आपको बताता है कि कौन-सी type का event हुआ।
Syntax
javascript
$(selector).on("eventName", function (event) {
  event.preventDefault();
  event.stopPropagation();
  event.target;
});

Event Type

हर event handler को एक event object मिलता है, और उसकी type property आपको exactly बताती है कि कौन-सा kind का event fire हुआ, तब उपयोगी जब एक function कई event types में shared हो।

उदाहरण: Event Type

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Click or hover</button>
    <script>
      $("#btn").on("click mouseenter", function(event) {
      console.log("Event type:", event.type);
      });
    </script>
  </body>
</html>

Target Element

event.target वह original element है जिसे event मिला, जो असल में आपने जिस पर handler attach किया उसके अंदर गहराई से nested कोई child element हो सकता है।

उदाहरण: Target Element

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box"><span>Inner text</span></div>
    <script>
      $("#box").on("click", function(event) {
      console.log("Target:", event.target.tagName);
      });
    </script>
  </body>
</html>

Current Target

event.currentTarget हमेशा वह element होता है जिस पर handler सीधे attach किया गया था, जो event.target से अलग हो सकता है जब event किसी descendant से bubble हुआ हो -- यह अंतर delegated events में सबसे ज़्यादा मायने रखता है।

उदाहरण: Current Target

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box"><span>Inner text</span></div>
    <script>
      $("#box").on("click", function(event) {
      console.log("currentTarget:", event.currentTarget.id, "target:", event.target.tagName);
      });
    </script>
  </body>
</html>

Mouse Coordinates

Mouse events clientX और clientY जैसी coordinate properties ले जाते हैं, आपको event fire होने के moment browser viewport के relative pointer की position देते हुए।

उदाहरण: Mouse Coordinates

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="pad" style="height:100px">Move mouse here</div>
    <script>
      $("#pad").on("mousemove", function(event) {
      console.log("X:", event.clientX, "Y:", event.clientY);
      });
    </script>
  </body>
</html>

Prevent Default

event.preventDefault() call करना उस event के लिए browser की normal built-in action को रोक देता है, जैसे किसी link के href को follow करना या किसी form को submit करना, event को आगे propagate होने से रोके बिना।

उदाहरण: Prevent Default

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <a id="link" href="https://example.com">Click me</a>
    <script>
      $("#link").on("click", function(event) {
      event.preventDefault();
      console.log("Navigation stopped");
      });
    </script>
  </body>
</html>
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. event.target इस्तेमाल करना जब आपका मतलब उस element से हो जिस पर handler bound है; अगर user #box के अंदर एक <span> जैसे child को click करता है, तो event.target span है जबकि event.currentTarget #box है।
  2. event parameter accept करना भूल जाना, function() { console.log(event.type); } लिखना और function(event) के बजाय पुराने global window.event पर निर्भर रहना।
  3. event.preventDefault() call करना और यह मान लेना कि यह bubbling भी रोक देता है, जबकि event को parents तक जाने से रोकने के लिए event.stopPropagation() चाहिए।

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.