Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-31-2007, 05:59 PM   PM User | #1
Bill999
New to the CF scene

 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Bill999 is an unknown quantity at this point
How to get the ID of the control in focus

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

Last edited by Bill999; 01-31-2007 at 06:42 PM.. Reason: Added missing onkeydown="IEDownKeyCapture()"
Bill999 is offline   Reply With Quote
Old 01-31-2007, 07:04 PM   PM User | #2
otaku149
Regular Coder

 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
otaku149 is an unknown quantity at this point
To kill the tab key for the HtmlBox1 control, add the following code into your Page_Load:
Code:
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
__________________
Martial
http://getElementById.com
otaku149 is offline   Reply With Quote
Old 01-31-2007, 07:51 PM   PM User | #3
Bill999
New to the CF scene

 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Bill999 is an unknown quantity at this point
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
Bill999 is offline   Reply With Quote
Old 01-31-2007, 08:18 PM   PM User | #4
otaku149
Regular Coder

 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
otaku149 is an unknown quantity at this point
Quote:
Originally Posted by Bill999
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:
Code:
<!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>
__________________
Martial
http://getElementById.com

Last edited by otaku149; 02-01-2007 at 01:59 AM..
otaku149 is offline   Reply With Quote
Old 02-01-2007, 03:33 PM   PM User | #5
Bill999
New to the CF scene

 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Bill999 is an unknown quantity at this point
Thanks again for your quick and helpful response. I really appreciate it.
Bill
Bill999 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:44 PM.


Advertisement
Log in to turn off these ads.