0

Possible Duplicate:
Detecting support for a given JavaScript event?

having a random HTMLElement el, is there anyway of knowing whether el can listen to an particular event type, for example, 'change'?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
cc young
  • 18,939
  • 31
  • 90
  • 148

2 Answers2

0

you can try checking if el.attributes.onchange is declared. I'm not sure, however, if dynamic changes to the webpage will be visible in this manner.

Eran Zimmerman Gonen
  • 4,375
  • 1
  • 19
  • 31
  • there is a property `el.onchange` == `null`, meaning it can be there, as versus `el.onchangexxx` == `undefined`, meaning it won't happen. the problem is that even DIV's have `onchange==null`, so this does not help anything. – cc young Aug 21 '11 at 15:41
  • 1
    @cc young, keep in mind that the [change event bubbles](http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-htmlevents), which means that a DIV can _listen_ for a change event that is triggered by one of its children. If you're asking how to detect whether an element can _trigger_ an event, that's a different question. – Cheran Shunmugavel Aug 21 '11 at 17:56
0

Yes, you can check it for a truthy value like this:

if (el.change) {
    el.change();
}

If the method exists you will be able to call it like this. You can check for any method in the manor before trying to call it.

Alexander Kahoun
  • 2,458
  • 24
  • 36
  • what if the change trigger has not yet been instantiated? what I'm trying to find is if I _can_ add a change trigger, ie, that is is a form element. – cc young Aug 21 '11 at 15:33