Virtual keyboard focus

S

sconeek

hi all,

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;

The problem is that i dont want to hard code a text field name. it
should type in text wherever the focus is.

can somebody suggest a generic solution. thanks.
 
R

Randy Webb

(e-mail address removed) said the following on 12/19/2005 11:50 PM:
hi all,

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;

The problem is that i dont want to hard code a text field name. it
should type in text wherever the focus is.

And it will do that. Unless you are trapping key events. Are you?
can somebody suggest a generic solution. thanks.

A URL to a sample page would be a great help.
 
I

Ivo

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/
 
R

Randy Webb

Ivo said the following on 12/20/2005 1:39 AM:
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.

A better alternative is a genuine name attribute :)
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;

If using the forms collection, using a name is better because of better
support:

document.forms['elementNAMEnotID'].elements['elementNAMEnotID']

Just my two cents worth at 7AM :)
 
S

sconeek

thanks to all. ivo -> i will try your approach and see if it helps. in
the meantime i am still looking for any other reference to this focus
problem.

thanks to all you geniuses for help. any more feedback appreciated.
 
S

sconeek

ivo->tried your approach. but its not working. i get an error message
when i focus on the second text field - Object expected. can somebody
help.
 
S

sconeek

i have done this so far,

function vk_data(x) {
var focused = document.forms[0].elements['usernameField'];
window.onload = function() {
document.forms[0].elements['usernameField'].onfocus() = function() {
alert("a");
focused = document.forms[0].elements['usernameField'];
}
document.forms[0].elements['passwordField'].onfocus() = function() {
focused = document.forms[0].elements['passwordField'];
alert("b");
}
}
focused.value+=x;
}

But it doesnt seem to work. I am calling vk_data on click of a button
(onClick) which is for diff characters. however its always focused
under username field and everytime any character button is pressed it
enters it in the username field. even if i click on password field it
still enters it within username field.

can somebody please show me how to enter in fields wherever the focus
is. thanks.
 
I

Ivo

i have done this so far,

function vk_data(x) {
var focused = document.forms[0].elements['usernameField'];

Earlier in the thread we said focused was going to be a global variable. By
using the var keyword here you make focused local to the vk_data function,
and its value, which we want to remember, will be lost when that function
returns.
window.onload = function() {

It 's certainly possible to assign event handlers over and over, but keeping
this onload setting in the global scope will prevent such waste. Move it out
of the function and it will run only once, just enough.

document.forms[0].elements['usernameField'].onfocus() = function() {

When setting onfocus or other handlers, don't use the brackets. They attempt
to make it a call to a method instead.
alert("a");
focused = document.forms[0].elements['usernameField'];
}
document.forms[0].elements['passwordField'].onfocus() = function() {
focused = document.forms[0].elements['passwordField'];

When event handlers are called directly like here, they have the 'this'
keyword available to them as a reference to the object that fired the event.
In other words, the above function needs no more than "focused = this".

See if that makes any differnce.
hth
ivo
http://4umi.com/web/javascript/
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top