<sc*****@gmail.com> wrote
i have a html form with multiple text fields, accepting input from a
virtual onscreen keyboard. so far i have
document.forms[0].elements['usernameField'].value+=x;
Don't refer to forms by index number, general good practice has every form
named with a genuine id attribute.
The problem is that i dont want to hard code a text field name. it
should type in text wherever the focus is.
So you tell each textfield to call a function onfocus, which in that fashion
registers which element last received the focus, and accordingly maintains a
global variable to retrieve a reference to the element from later on when
you detect a keypress. By setting this variable to some element during its
declaration, you can supply a 'default' element to type in and remove the
need to check if it is set when the user first starts typing.
var focused = document.forms.f.elements.usernameField;
window.onload = function() {
var el = document.forms.f.elements; // form with id=f
el.usernameField.onfocus = el.yetanotherField.onfocus =
function() { focused = this; }
}
window.document.onkeydown = function () {
// you know to get x
focused.value += x;
}
hth
ivo
http://4umi.com/web/javascript/