Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
4.40/5 (2 votes)
See more:
Can any one suggest me , How to Set Focus on Textbox using Javascript in Asp.net When i press TAB button .

C#
if(document.getElementById("Textbox1").value=="")
    {
        alert ( 'Invalid Date' );
        document.getElementById('Textbox1').value='__/__/____';
        document.getElementById('Textbox1').focus();
    }


The Above coding is the Javascript which i used in my Form . This coding is working Properly but if the textbox1 is NULL and if i press Tab button from Textbox1 means its Working(ie., POPUp Message is coming) but its NOT Focusing on the Textbox1 itself .

Thanks in Advance.


Regards,
SARAVANAN.M
Posted
Updated 5-Jun-11 20:31pm
v3
Comments
cuteband 6-Jun-11 2:01am    
if(document.getElementById("Textbox1".value=="")
Because of this condition. Thats the reason its not done the focus
SARAVANAKUMAR.M 6-Jun-11 2:04am    
Sorry , now the Question is Correct, it was my mistake while Typing the Question.


First bug here is in the first line, must be:

JavaScript
var textBox = document.getElementById("Textbox1");
if (textBox)
   /*...*/


—SA
 
Share this answer
 
Comments
SARAVANAKUMAR.M 6-Jun-11 2:10am    
I tried that also but its not Focusing on that Textbox1 itself .
Sergey Alexandrovich Kryukov 20-Jun-12 11:07am    
Could you run the script under debugger then?
--SA
Sarathkumar Nallathamby 11-Dec-12 2:55am    
This Code is working well in IE only..While i am invoking the script from onblur, then the alert only coming . But focus is not coming ..
I am using FireFox version 13..
XML
<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="Textbox1" runat="server"></asp:TextBox>
    </div>
    <script language="javascript" type="text/javascript">
        if (document.getElementById("Textbox1").value == "") {
            alert("Invalid date");
            document.getElementById('Textbox1').value = '__/__/____';
            document.getElementById("Textbox1").focus();
        }
    </script>
    </form>


It is working properly for me.
 
Share this answer
 
v2
Comments
SARAVANAKUMAR.M 6-Jun-11 2:51am    
I tried this also but its not Focusing on that Textbox1 itself .
use this code

<script language="javascript" type="text/javascript">
        if (document.getElementById("Textbox1").value == "") {
            alert("Invalid date");
            document.getElementById('Textbox1').value = '__/__/____';
            document.getElementById("Textbox1").focus();
        }


It is working fine for me.
 
Share this answer
 
v4
Comments
SARAVANAKUMAR.M 6-Jun-11 2:56am    
I tried this also but its not Focusing on that Textbox1 itself . i thnik it might be some problem ?
try to put : ClientIDMode="Static" on TextBox.

ASP.NET
<asp:textbox id="Textbox1" runat="server" clientidmode="Static" xmlns:asp="#unknown"></asp:textbox>


Good Luck :)
 
Share this answer
 
U need to debug the javascript code for that. Thereafter, u'll know where's the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900