I had to develop a solution where the user could press enter or return from an input field and the correct submit button would be clicked. We could only use one form for the whole page.
This can be added to an external js file.
//In the onFocus event of the form input text element call the function
setSubmitBtn with the following parameters setSubmitBtn(form id, button id) e.g.
onfocus="setSubmitBtn('frmHitList', 'btnSave')"
//Global variable for the submit button set.
//Used by functions setSubmitBtn and keyPressed.
var submitBtnSet = null;
var btnFormId = null;
//Checks if the key pressed is the enter/return key
//If it is then click the button set by setSubmitBtn event
function keyPressed (Event) {
if (!Event)
Event = window.event;
var keyCodePressed = Event.keyCode;
if (keyCodePressed == 13) {
if (submitBtnSet != null) {
document.forms[btnFormId].elements[submitBtnSet].click();
submitBtnSet = null;
btnFormId = null;
return false;
}
}
}
//Set the submit button name which should be clicked when the user
//presses the enter or return key
function setSubmitBtn(formId, submitBtn) {
btnFormId = formId;
submitBtnSet = submitBtn;
}
//Calls the keyPressed function with every key pressed
document.onkeypress = keyPressed;
Tuesday, September 19, 2006
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment