Jump to content

Form focus.


waynew

Recommended Posts

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.