You are not Logged in
Would you like to Login or Register

Today is: Friday, 21 November, 2008
Check this months hot topics

TAB inside input boxes

When you want to insert a TAB character inside a textarea (or a textbox) in a web page, you press TAB key and the focus moves to another field in the page instead of a TAB character being inserted into the textarea. The problem is that TAB key is reserved for web pages to navigate through the objects that can be focused (or blurred) inside the web page.

TAB characters are especially useful when you want to display formatted text in your HTML text field. In order to solve this problem, we need to override one of onkey related events of the text field so that the TAB character would be inserted where the cursor stays instead of the text field losing the focus to the next control in the web page. There are three onkey related events: onkeydown, onkeyup and onkeypress for an HTML text field. However, onkeypress only fires for some special character set and it does not fire for the TAB key. Therefore, we can only use onkeydown or onkeyup. We will use onkeydown event since it fires as soon as the user presses a key, not when the user releases a key.

Define onkeydown event for the asp:textbox controls in <BODY> section of your ASPX page as follows:

<asp:textbox id="TextBox1" runat="server" width="520px">
    Press TAB key to move the focus to the next field when the cursor is here...
</asp:textbox><br>
<asp:textbox id="TextBox2" runat="server" onkeydown="HandleKeyDown(this);" width="520px">
    Press TAB key to insert a tab character into this textbox when the cursor is here...
</asp:textbox><br>
<asp:textbox id="TextArea1" runat="server" onkeydown="HandleKeyDown(this);" width="520px" height="200px" textmode="MultiLine">
    Press TAB key to insert a tab character into this textarea when the cursor is here...
</asp:textbox>

Then, add the following JavaScript code into <HEAD> section of your ASPX page:

<script language="javascript">
//<!--
function HandleKeyDown(obj)
{
    var tabKeyCode = 9;
    if (event.keyCode == tabKeyCode && event.srcElement == obj)
    {
        obj.selection = document.selection.createRange();
        obj.selection.text = String.fromCharCode(tabKeyCode);
        event.returnValue = false;
    }
}
//-->
</script>

The above code will only work for IE browsers. You will need to handle the onkeydown event differently for other browsers.

kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Barrington Haynes Skype
Author : Barrington Haynes
Published : Tuesday, 13 June, 2006

Barry is the coolest member of the Innovation Team - not only does he code he's also a rock star (lead vocals and guitarist in the bands BridgeFire and Best of Foo) - what a combination. Barry is extremely interested in the integration of software components and embraces a lot of the current hype around web 2.0 and related concepts as standard.

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views

More Publications

Not using Google Analytics? You should be!
Paul Marshall - 23/8/2006
TAB inside input boxes
Barrington Haynes - 13/6/2006
Web service calls from javascript using ATLAS (part 1)
Dave Howard - 25/8/2006
Image reflection the easy way!
Andrew Clark - 5/4/2007
Web service calls from javascript using ATLAS (part 3) - Complex properties
Dave Howard - 25/8/2006