Tuesday, June 9, 2015

List all bindings of an element (with jQuery)

<html>
  <head>
    <script src="jquery-1.11.3.min.js"></script>
    <script>
      $(document).ready(function() {
        $('.MyElement').on('click', function(e){
          e.preventDefault();
          console.log('ok 1');
        });

        $('.MyElement').on('click', function(e){
          e.preventDefault();
          console.log('ok 2');
        });

        console.log('Number of element in this selector: ' + $('.MyElement').get().length);

        $.each($._data($('.MyElement').get(0), 'events'), function(i, e) {
          console.log(i, e);
        });
      });
    </script>
  </head>
  <body>
    <a href="#" class="MyElement">test 1</a><br>
    <a href="#" class="MyElement">test 2</a><br>
    <a href="#" class="MyElement">test 3</a><br>
  </body>
</html>

Reference:

http://stackoverflow.com/questions/4138543/list-all-bindings-of-an-element-with-jquery
http://stackoverflow.com/questions/2518421/jquery-find-events-handlers-registered-with-an-object
http://blog.ijun.org/2014/11/quickly-finding-and-debugging-jquery.html

No comments: