Hope there's a simple workaround for this. I want to select all the html elements with event attributes. For example: onclick, onkeypress etc. Is there an easiest way to do this using Jquery without selecting by each attribute seperately?
Thanks
I believe that the short answer to your question is no.
Different HTML tags support different events, so they should be hardcoded somewhere in the jQuery code. And reading through the jQuery code, I cannot find any reference to onkeypress event, for example.
So, I think you can just rely on Has Attribute Selector [attribute]
:
$('[onclick], [onkeypress], [etc]');
You could make a custom filter function to find elements with an attribute that starts with on , like so:
$.fn.filterOn = function() {
this.each(function(ind,el) {
var attrs = el.attributes;
for (var i = 0; i < attrs.length; i++) {
if (attrs[i].nodeName.indexOf('on') === 0) return true;
}
return false;
});
};
and use it like:
//elems will contain all input fields with an attribute starting with 'on'
elems = $(':input').filterOn();
And this will give you ALL elements in the page that has an attribute starting with on (beware of performance when using * selector):
$("*").filterOn().each(function() {
console.log('Element '+this.tagName + ' has an on... attribute');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With