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.)

1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Event delegation is definitely the way to go. This way, you don't pollute the prototype, and don't create tons of event handlers.
Do DOM objects inherit any common "superclass"?
@Paul: Yes, but it won't help.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.