By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

Virtual keyboard focus

P: n/a
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.

Dec 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
sc*****@gmail.com 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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 20 '05 #2

P: n/a
Ivo
<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/
Dec 20 '05 #3

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


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


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

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 20 '05 #4

P: n/a
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.

Dec 20 '05 #5

P: n/a
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.

Dec 20 '05 #6

P: n/a
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.

Dec 21 '05 #7

P: n/a
Ivo
<sc*****@gmail.com> wrote
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/
Dec 21 '05 #8

P: n/a
On 2005-12-20, sc*****@gmail.com <sc*****@gmail.com> wrote:
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.


store the focussed element to a global variable using the onfocus event of
each field.

Bye.
Jasen
Dec 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.