Forum Moderators: open

Message Too Old, No Replies

Detecting form field focus

         

fintan

3:07 pm on Dec 7, 2007 (gmt 0)

10+ Year Member



Hi I'm trying to detect what form field has focus. Is there an easy way to do it? Thanks

fintan

function cancel_submit(e, form_sub){ // Cancels the enter behaviour for submiting forms unless submit is pressed

var formObj = document.forms[0]; //forms[0];

if(EnterPressed(e)) {
for(var i = 0; i < formObj.length; i++){
if(formObj.elements[i].type == 'submit' && formObj.elements[i].focus()) {
alert(formObj.elements[i]);
}
}
return false;
}
else{
return false;
}
}

gergoe

2:17 am on Dec 8, 2007 (gmt 0)

10+ Year Member



You don't necessarily need to know that if you are only interested about the submit button. Add two events to your submit button: onfocus and onblur. In the onfocus set a global variable like submitHasFocus to true, and in the onblur, set the same variable to false. Then in your cancel_submit function only need to check this variable.

On the other hand, if you don't add a "submit" button to your form, then you only need to make one normal button, which will have document.form.submit() in its onclick event. In this case the user can not initiate the submitting anymore, but you can still do that from the code.