How to get the object that the client click?

C

Cylix

At the old method, I place onFocus() event of all the tags like this:
<input id="input1" type="text" onFocus="func(this)">
to get the source (sender) of object.
But I think this is not a good design style for some case,
for example, I have fifty inputs in a HTML and half of it should run a
function when focus.

How to write this task better?
 
V

Vincent van Beveren

How to write this task better?
Depends on what you want. If each input element has its own type of
handling, then the best option is what you describe, if its the same
handler you could use a loop:

// define the generic event handler
function myEventHandler() {
this.value = 'I have focus';
}

// get all input elements
inputs = document.getElementsByTagName("input");

// add the event handler to each of them.
for (i = 0; i < inputs.length; i++) {
inputs.onfocus = myEventHandler;
}

Something like that. You can even decide in the loop whether or not this
input element should have an event handler, and if so, which one.

Vincent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top