Click to See Complete Forum and Search --> : Which element has focus


Igor
04-14-2003, 07:45 PM
Hi everybody,
Is there a way to find out whitch element has focus on DHTML form with javascript?
Thanks
Igor

Michael
04-21-2003, 08:07 PM
Igor,

You can use (and modify) this JavaScript for your needs.

function testForFocus()
{
if (document.myForm.myTxtBox.gotfocus = true)
{
alert("Text box has focus!")
}
}

This script is assuming the form name is "myForm" and the text type input is
named "myTxtBox".

Michael Sanchez
Runtime Web Development


"Igor" <irodionov@hotmail.com> wrote in message
news:3e9b2be5$1@tnews.web.devx.com...
>
> Hi everybody,
> Is there a way to find out whitch element has focus on DHTML form with
javascript?
> Thanks
> Igor

Tim Ellison
10-23-2003, 03:55 PM
Here's another approach that doesn't care about the element.

function getElementWithFocus()
{
var rtnValue = null;
for(var i=0;i<document.all.length;i++)
{
try
{
if ( document.all.items[i].gotFocus )
{
// assume that element has name and/or id.
alert( document.all.item[i].name + ' has focus!');
rtnValue = document.all.items[i];
break;
}
}
catch (ex)
{
// catch exception for when the element doesn't support the gotFocus
attribute.
}
return rtnValue;
}
}

--

Regards,

Tim Ellison, MCP
Whitlock eBusiness Solutions
(w) 804-794-2871 x144
(m) 804-405-4874

"Igor" <irodionov@hotmail.com> wrote in message
news:3e9b2be5$1@tnews.web.devx.com...
>
> Hi everybody,
> Is there a way to find out whitch element has focus on DHTML form with
javascript?
> Thanks
> Igor