0

I can't get an event listener in Chrome DevTools and Firefox DevTools when creating a different alias instead of jQuery to use jQuery.noConflict

I would like to know how to get event listeners in Chrome DevTools or Firefox DevTools when using jQuery noConflict.

<script src="https://code.jquery.com/jquery-3.7.0.js"></script>

<!--
external js file that I cannot change.
<script src="external.js"></script>
-->
<script>
//Contents of external.js
  let $j = $.noConflict(true);
  (function($) {
    $(function() {
      init();
    });
    function init() {
      $('.click').on('click', function() {
        console.log('click');
        return false;
      });
    }
  })($j);
  $j = null;
</script>

<a href="#" class="click">click</a>

DevTools

  • 1
    See https://stackoverflow.com/questions/2518421/jquery-find-events-handlers-registered-with-an-object for how you get jQuery event handlers. Since you're using `noConflict`, use `$j` instead of `$` or `jQuery`. – Barmar Jul 13 '23 at 01:45
  • Thank you for your comment. I am very sorry. The code was wrong so I changed it. – eternal novice Jul 13 '23 at 02:03
  • Before the code change, the handler was displayed by typing the following in the console. `$j._data($j('a').get(0), 'events').click[0].handler` – eternal novice Jul 13 '23 at 02:11
  • Why did you reassign `$j`? Now there's no way to access that version of jQuery outside the anonymous function. – Barmar Jul 13 '23 at 02:11
  • The reason is unknown because it is an external file.There's no way to access it. . . I understand. Thank you for your teachings. – eternal novice Jul 13 '23 at 02:18

1 Answers1

1

Set a breakpoint after $j = $.noConflict(true);. Then you can manually assign some other variable to hold a reference to this version of jQuery:

myJQuery = $j;

Then you can use myJQuery in place of $ or jQuery to access the shadowed version of jQuery.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you for your answer. I couldn't figure out how to get the event handler.`myJQuery._data(myJQuery('a').get(0), 'events').click[0].handler ` was not available. – eternal novice Jul 13 '23 at 04:45