Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 07:42 PM. Reason: Added missing onkeydown="IEDownKeyCapture()"

  • #2
    Regular Coder
    Join Date
    May 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    May 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>
    Last edited by otaku149; 02-01-2007 at 02:59 AM.

  • #5
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks again for your quick and helpful response. I really appreciate it.
    Bill


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •