24

So I am trying to determine whether its possible to listen for events added with jQuery using vanilla JS. I found this question:

Listen to jQuery event without jQuery

which definitely answers it for version 1 of jQuery. How about version 3 however?

I have a fiddle that I have put together to test out, but I am unable to get the 1st submit to work with any version of jQuery. Am I missing something, or is the event model in jQuery 3 still not using the DOM event model?

https://jsfiddle.net/ydej5qer/1/

Here is the code in the fiddle:

HTML:

<div id="div1">
<p>
This is div1. My event was added via jQuery and is listened for by vanilla JS.
</p>
<p>
  Enter the number 2 to have the event fired.
</p>
<input type="text" id="input1" />
<button id="button1">
Submit
</button>
</div>
<div id="div2">
  <p>
    This is div2. My event was added via vanilla JS and is listened for by jQuery.
  </p>
  <p>
    Enter the number 2 to have the event fired.
  </p>
  <input type="text" id="input2" />
  <button id="button2">
  Submit
  </button>
</div>

JavaScript:

var $input1 = $("#input1");
var $input2 = $("#input2");
var input1 = document.getElementById("input1");
var input2 = document.getElementById("input2");

var event1 = "event1";
var event2 = "event2";

$("#button1").click(function() {
    if (+$input1.val() == 2) {
    $input1.trigger(event1, {message: "Event 1 triggered!"});
  }
});

input1.addEventListener(event1, function(e) {
    console.log("Event 1 triggered! message=" + e.detail.message);
});

$("#button2").click(function() {
    if (+$input2.val() == 2) {
    var event = new CustomEvent(event2, {detail: {message: "Event 2 triggered!"}});
    input2.dispatchEvent(event);
  }
});

$input2.on(event2, function(e) {
    console.log("Event 2 fired, but I don't know how to get the message!");
});
thatidiotguy
  • 8,701
  • 13
  • 60
  • 105
  • For the "is it possible" part, I'd dare say it is. jQuery is not a binary component or anything esoteric, it's just a bunch of Vanilla JavaScript code. ;-) – Álvaro González Dec 01 '16 at 16:05
  • @ÁlvaroGonzález What exactly are you proposing? How would vanilla JS access private jQuery event structures? Are you proposing I modify the core library? – thatidiotguy Dec 01 '16 at 16:52
  • I'm not proposing anything (please note it is a comment followed by a smiley, not an answer). Sorry if I suggested otherwise. – Álvaro González Dec 01 '16 at 16:55
  • See http://stackoverflow.com/a/18901932/673457 – Ted Whitehead Dec 01 '16 at 17:15
  • @TedWhitehead That case references the exact same bug report that is included in the case I linked in my question. That question offers no new information about jQuery 3. – thatidiotguy Dec 01 '16 at 17:52
  • Ah, sorry, I skipped over that link and went straight to the code snippet ;) – Ted Whitehead Dec 01 '16 at 18:32
  • I think this might be what you’re looking for http://stackoverflow.com/a/21292403/673457 – Ted Whitehead Dec 01 '16 at 18:46
  • @TedWhitehead While I appreciate the comment, this does not really help. I already knew that I could just trigger events in native JS. My question is whether jQuery 3 natively provides a mechanism that allows its events to be handled by vanilla JS, without actually dispathing native events. You can see from my fiddle that I know that it is possible :) – thatidiotguy Dec 01 '16 at 19:28
  • The link I posted was meant to show that it’s not possible to listen to jQuery event without using jQuery. “jQuery's event handling system is a layer on top of native browser events” http://learn.jquery.com/events/triggering-event-handlers/ I think you would need to create a new custom trigger() method that emits both jQuery and native events. – Ted Whitehead Dec 01 '16 at 19:55

2 Answers2

41

The short answer is that this is impossible as jQuery provides an event layer over vanilla JS. That means that vanilla JS cannot talk to that added layer.

So in summary, jQuery can catch vanilla JS events, but vanilla JS cannot catch jQuery added events.

thatidiotguy
  • 8,701
  • 13
  • 60
  • 105
2

This question is somewhat old, but it still ranks highly, so for anyone looking for a way to listen to jQuery events with vanilla JS: It is now possible with the aptly named jquery-events-to-dom-events library.

janh
  • 2,885
  • 2
  • 22
  • 21