How to set 'onKeyPress ...' programmatically ?

M

Mel Smith

Hi:

Enviro: >50 input text fields

Question: How does one set _programmatically_ in javascript, the onKeyPress
event listener ??

I tried a couple of statements (something like el.onKeyPress = myfunction()
; and "myfunction", etc, etc.

Everything craps out and I don't know Where in my JavaScript Bible to look
for the answer ??

A Hint please ??

Thank you
 
E

Evertjan.

Mel Smith wrote on 18 jan 2012 in comp.lang.javascript:
Hi:

Enviro: >50 input text fields

Question: How does one set _programmatically_ in javascript, the
onKeyPress event listener ??

I tried a couple of statements (something like el.onKeyPress =
myfunction() ; and "myfunction", etc, etc.

Everything craps out and I don't know Where in my JavaScript Bible to
look for the answer ??

A Hint please ??

something like, [not tested]:

var inputs = document.forms[0].getElementsByTagName('input');
for (i=0;i<inputs.length;i++)
inputs.onKeyPress = myFunction;
 
M

Mel Smith

Evertjan. said:
something like, [not tested]:

var inputs = document.forms[0].getElementsByTagName('input');
for (i=0;i<inputs.length;i++)
inputs.onKeyPress = myFunction;


Hi Evertjan:

Here is what I started with below

It still shows undefined for el.onKeyPress.

Any more guidance please ?

Thank you for responding.

-Mel Smith


************* my original code *******
function setonpress() {
var el ;
var inputs = document.getElementsByTagName("input") ;

// now we have to add a keypress function to each of them
for (var i=0;i<=inputs.length;i++) {
el = inputs ;
if (el.type === "text") {
window.alert("type of el.onKeyPress = "+ typeof el.onKeyPress) ; //
always results in 'undefined'
// I wish the focusOnElement function to be 'planted' in each of my
50+ text fields
// and I don't what to have to type them in.
el.onKeyPress = focusOnElement ; // so this craps out :((
return true; // get out on 1st field for testing purposes. //
I leave quickly while testing
} ;
}
}

*******************************
 
T

Tim Streater

Mel Smith said:
************* my original code *******
function setonpress() {
var el ;
var inputs = document.getElementsByTagName("input") ;

// now we have to add a keypress function to each of them
for (var i=0;i<=inputs.length;i++) {
el = inputs ;
if (el.type === "text") {
window.alert("type of el.onKeyPress = "+ typeof el.onKeyPress) ; //
always results in 'undefined'


Well you've not added it yet, have you?
// I wish the focusOnElement function to be 'planted' in each of my
50+ text fields
// and I don't what to have to type them in.
el.onKeyPress = focusOnElement ; // so this craps out :((

What do you mean by "craps out"?
 
M

Mel Smith

Tim Streater said:
What do you mean by "craps out"?


Hi Tim & Evertjan:

Yes, you are (of course) correct !

It occurred to me as I gave up in disgust, then returned a couple of
minutes ago, and tried it out with an alert *after* the assignment.

now, it returns 'function' as I wanted it to :))

Now,. can I pass an argument to the function (e.g.,
focusonElement(this) ).

Anyway, I'll try it tomorrow and see what happens

Thank you both very much !

-Mel Smith
 
E

Evertjan.

Mel Smith wrote on 19 jan 2012 in comp.lang.javascript:
Evertjan. said:
something like, [not tested]:

var inputs = document.forms[0].getElementsByTagName('input');
for (i=0;i<inputs.length;i++)
inputs.onKeyPress = myFunction;


Hi Evertjan:

Here is what I started with below


If something does not work, don't start with such complex code,
try the most simple one, and then go debugging

It turns out that the onkeypress should be all lowercase:

Tested in Chrome:
============================
<input id=a><br>
<input id=b><br>
<input id=c><br>

<script type='text/javascript'>
var inputs = document.getElementsByTagName('input');
for (i=0;i<inputs.length;i++)
inputs.onkeypress = f;

function f() {
alert(this.id);
};
</script>
============================
 
M

Mel Smith

Evertjan. said:
If something does not work, don't start with such complex code,
try the most simple one, and then go debugging

It turns out that the onkeypress should be all lowercase:

Tested in Chrome:
============================
<input id=a><br>
<input id=b><br>
<input id=c><br>

<script type='text/javascript'>
var inputs = document.getElementsByTagName('input');
for (i=0;i<inputs.length;i++)
inputs.onkeypress = f;

function f() {
alert(this.id);
};
</script>
============================



Hi Evertjan:

Thanks for the code

Will try again tomorrow -- I have a granddaughter who has to be taken to
the airport in a few minutes.

-Mel
 
J

J.R.

Hi:

Enviro:>50 input text fields

Question: How does one set _programmatically_ in javascript, the onKeyPress
event listener ??

I tried a couple of statements (something like el.onKeyPress = myfunction()
; and "myfunction", etc, etc.

Everything craps out and I don't know Where in my JavaScript Bible to look
for the answer ??

A Hint please ??

Thank you

Hi,
You might use:
el.onkeypress = myfunction; // neither onKeyPress nor myfunction()

Take a look at MSDN to see more details:
<http://msdn.microsoft.com/en-us/library/ms536939(v=vs.85).aspx>
 
D

Dr J R Stockton

Wed said:
Question: How does one set _programmatically_ in javascript, the onKeyPress
event listener ??

PLUS : Just in case, and if you are testing in one browser and hoping to
run in all browsers, you should know about e & event in the following
working code; but I am not the one to actually explain it.

Rag.onmousedown = function (e) {
e = e || window.event
OldX = e.clientX ; OldY = e.clientY ; Doon = true }
 
M

Mel Smith

DR. J said:
Rag.onmousedown = function (e) {
e = e || window.event
OldX = e.clientX ; OldY = e.clientY ; Doon = true }

Hi Dr. Stockton:

My user (just one of them for now) is the office assistant who runs my
web app (which I'm building now) in the office of our senior park.

From gazing at my Apache access log, she currently runs IE9 , and
perhaps a slightly older IE from home -- which she doesn't use much. (We
are both used to communicating solely by email since I spend 7 months of the
year 3000 Km to the north)

Moving about the 50+ text fields (with only <Tab> and <Sh-Tab> will be
annoying to her. She is used to <CR>, and the Up/Down arrows to move around
a data entry screen. So, <Tab> and <Sh-Tab> are not real options for this
app

The web app is a Lot Management System (613 lots) with a home on the
lot, and owners, renters, lessors, as occupants, and maybe up to four local
(or remote) owners, and a lot of other contact infor on phones, email
addresses, full addresses (local and remote), etc, etc. Then, there's the
non-owner occupancy fee, and how it is collected and reported uipon, etc.

... Now, I'm scaring the Hell out of myself :(

But anyway, I understand that events are treated differently among the
browsers, and I'll be searching in my "Javascript Bible" for guidance.

But, I don't understand your code above, and its late in the day, and
I'm tired out from golfing : (((

Thanks for the post !

-Mel
 

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
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top