waynew Posted June 27, 2008 Share Posted June 27, 2008 Hey guys. Could somebody help me on this? Is there any JavaScript code to figure out what current form is in focus? I've been Googling for this and nothing seemsto help. I don't want to put the focus on a form, I want to know what form is in focus. Any help would be great! Quote Link to comment https://forums.phpfreaks.com/topic/112198-form-focus/ Share on other sites More sharing options...
xtopolis Posted June 27, 2008 Share Posted June 27, 2008 A possible solution... I'm not sure if this will work, but it might: So I have this cross browser code for mouse events... and you see the last part: e.target returns an element. See code, then read more. <script type="text/javascript"> function MouseEvent(e) { if(e) { this.e = e; }else{ this.e = window.event; } if(e.pageX) { this.x = e.pageX; }else{ this.x = e.clientX; } if(e.pageY) { this.y = e.pageY; }else{ this.y = e.clientY; } if(e.target) { this.target = e.target; }else{ this.target = e.srcElement; } } </script> So I'm thinking, since for a form to be in focus, an element ?has? to be selected..? It's possbile you could invoke this mouse event when you want to check and it may give you what you're looking for. Possibly <html><head><script type="text/javascript"> function getFormFocus() { var e = new MouseEvent(e); var x = e.target; } </script></head> <body onload="getFormFocus(event)"> Then from there you might be able to find out what the parent element is of the selected element in order to get the form name. For example, an input element might have the structure of ... document.Form1.input.. you may be able to backtrack that structure to get the value Form1. I do not know if it will work as you hope, but it might help. Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/112198-form-focus/#findComment-576390 Share on other sites More sharing options...
KevinM1 Posted June 28, 2008 Share Posted June 28, 2008 Hey guys. Could somebody help me on this? Is there any JavaScript code to figure out what current form is in focus? I've been Googling for this and nothing seemsto help. I don't want to put the focus on a form, I want to know what form is in focus. Any help would be great! Try: <script type="text/javascript"> window.onload = function() { var forms = document.forms; var focusedForm = ''; for(var i = 0; i < forms.length; i++) { forms[i].onfocus = function() { focusedForm = this.name; // or this.id, depending on your identification scheme } } // do something with focusedForm } </script> Quote Link to comment https://forums.phpfreaks.com/topic/112198-form-focus/#findComment-576530 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.