...

View Full Version : How to get the ID of the control in focus



Bill999
01-31-2007, 06:59 PM
Hello all,

I am programming a web app in Visual Studio 2005 (VB) which is using Dart's PowerWEB HTML Textbox.

I am trying to capture the Tab Key using onkeydown in jscript and if the HTML Textbox has the focus, I want to kill the tabkey.

My code looks like this:

<script language="javascript" type="text/javascript">
function IEDownKeyCapture()
{
if (window.event.keyCode == 9) {

var ControlInFocusStr = ?????? <--- This is where I need help

if (ControlInFocusStr == "HtmlBox1") {
event.returnValue = false; // This kills the tab
}
}
}
</script>
</head>
<body
onkeydown="IEDownKeyCapture()" >

<form id="frmApprovalForm" method="post" runat="server">
....
<cc1:HtmlBox ID="HtmlBox1" runat="server"></cc1:HtmlBox><br />
.....
</form>
</body>
</html>

Can anyone tell me how I can get the ID/Name of the control that has focus when the user presses the Tab Key?

Thanks for your help.
Bill

otaku149
01-31-2007, 08:04 PM
To kill the tab key for the HtmlBox1 control, add the following code into your Page_Load:


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

HtmlBox1.Attributes.Add("onkeydown", "if(event.keyCode==9){return false;}")

End Sub

Bill999
01-31-2007, 08:51 PM
Thank you.
That does what I needed for the present time.

But I also wanted to add 5 spaces to the text in the html text box once I found out that the focus was on the html text box. Is there any way to know which control was in focus when the tab key was pressed? Or is there another way to do this in the Page_Load?

Bill

otaku149
01-31-2007, 09:18 PM
Is there any way to know which control was in focus when the tab key was pressed?

Here is a small sample on how to get the id of the element:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get which element is in focus when the tab key is pressed</title>
<script type="text/javascript">
function getId(id){
if(event.keyCode==9){
alert(id);
return false;
}
}
</script>
</head>
<body>
<textarea id="HtmlBox1" onkeydown="return getId(this.id)">
</textarea>
<textarea id="HtmlBox2" onkeydown="return getId(this.id)">
</textarea>
<textarea id="HtmlBox3" onkeydown="return getId(this.id)">
</textarea>
</body>
</html>

Bill999
02-01-2007, 04:33 PM
Thanks again for your quick and helpful response. I really appreciate it.
Bill



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum