tags:

views:

29

answers:

1

Is it possible to define a event handler of one type of HTML object so that all the instances of this type of objects have this handler? for example, can I create a function func() as the onclick event handler of input text-fields so that all the input-fields have func() as its onclick event handler?

(it's just something like adding a function to the prototype of a native javascript object.)

+2  A: 

No.

Instead, you can handle onclick for the document, then check if e.target || e.srcElement is an <input>.

This is how jQuery's .live() works.

SLaks
Event delegation is definitely the way to go. This way, you don't pollute the prototype, and don't create tons of event handlers.
Matt Ball
Do DOM objects inherit any common "superclass"?
Paul
@Paul: Yes, but it won't help.
SLaks
The DOM prototype object behavior is inconsistent between implementations in my experience. I'd suggest not fiddling with extending the prototype unless you have a lot of spare time on your hands.
Scott S. McCoy