[SOLVED] How to add new method onKeydown to the text field

Hello,

I need onKeydown to be added to the field, because the planned event has to happen when user presses Enter. onChange method doesn’t work for me, because field loses focus, while I need cursor to stay in it in order to repeat this event again and again (to say, move field value to the Select list).
Thank you for any idea.

Does the onclick solve your issue?

Not actually, because it is more intuitive for user to press Enter after typing some value into the the text field to start validation and processing of this value (e.g. moving this value into the Select list in my case). Clicking the mouse on the field is less intuitive IMHO. I would better place special button next to the field, but in that case field loses focus, which is undesirable.

Hi,
maybe you could try something like the following in your “Javascript - form - onLoad” section.

function pressenter(e) {
var keycode;
if (window.event) {
keycode = window.event.keyCode; // IE
}else keycode = e.which; // FF

if (keycode == 13) {
document.F1.field1.value += document.F1.field2.value;
return false;
} else return true;
}

document.F1.field2.onkeypress=pressenter;

Hope this helps.
jsb

Hi jsbinca,

Sorry about probably silly question, but how do you pass parameter into the function pressenter()?
There is no parameter (event object, i guess?) when you call the function in the last line.

Thank you,

Hi,
there is no parameter for this function. It’s fired up on every keystroke in field2. If it is the Enter key CHR(13) it adds the value of field2 to the value of field1. That’s it.

jsb

Thank you, jsb. The code works great in any html/javascript emulator. The next problem for me is how to implement it into SC app. You recommended to put it into Javascript - form - onLoad" section. The good question for me is where to find the name of the control form used for referring input fields. F1 used in the example above is just a generic name, correct? I tried forms[0], but it doesn’t work.

Hi,
F1 is not a generic name it is the actual name of the form used by SC (Check the page source in the browser when your application is running). So you can use the code more or less as it is, you just have to adjust the name of the field variables (field1 is just a place holder) and probably the operation performed by the function.

jsb

Thanks a lot, jsb. It works perfect!