title: Can i get all attributes that begin with "on" using jquery?
body: <p>I am looking for a way to get all the attributes of an element that begins with "on" using jQuery or Vanilla JS. I am currently getting <strong>all</strong> attributes and then looping through them to get the ones I want using the method proposed by @primvdb on this post: <a href="https://stackoverflow.com/questions/14645806/get-all-attributes-of-an-element-using-jquery">Get all attributes of an element using jQuery</a>.</p>
  
  <p>My code looks like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>/* Expanding .attr as proposed by @primvdb */
  (function(old) {
    $.fn.attr = function() {
      if(arguments.length === 0) {
        if(this.length === 0) {
          return null;
        }
  
        var obj = {};
        $.each(this[0].attributes, function() {
          if(this.specified) {
            obj[this.name] = this.value;
          }
        });
        return obj;
      }
  
      return old.apply(this, arguments);
    };
  })($.fn.attr);
  
  /* And then my function */
  $.fn.attrThatBeginWith = function(begins){
    var attributes = this.attr();
    var attrThatBegin = {};
    for(var attr in attributes){
      if(attr.indexOf(begins)==0){
        attrThatBegin[attr] = attributes[attr];  
      }
    }
    return attrThatBegin;
  };
  
  /* Usage */
  var onAttributes = $("#MyElement").attrThatBeginWith("on");</code></pre>
  </div>
  </div>
  </p>
  
  <p>And this works but is very "dirty". It's seems like with all the vast features of jQuery there should be a better "cleaner" way to do this. Does anybody have any suggestions?</p>