Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
Can any one suggest me , How to Set Focus on Textbox using Javascript in Asp.net When i press TAB button .
 
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 5-Jun-11 19:58pm
Edited 5-Jun-11 20:31pm
v3
Comments
cuteband at 6-Jun-11 2:01am
   
if(document.getElementById("Textbox1".value=="")
Because of this condition. Thats the reason its not done the focus
SARAVANAKUMAR.M at 6-Jun-11 2:04am
   
Sorry , now the Question is Correct, it was my mistake while Typing the Question.
 
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First bug here is in the first line, must be:
 
var textBox = document.getElementById("Textbox1");
if (textBox)
   /*...*/
 
—SA
  Permalink  
Comments
SARAVANAKUMAR.M at 6-Jun-11 2:10am
   
I tried that also but its not Focusing on that Textbox1 itself .
Sergey Alexandrovich Kryukov at 20-Jun-12 11:07am
   
Could you run the script under debugger then?
--SA
sarathkumarnallathambi at 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..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

<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.
  Permalink  
v2
Comments
SARAVANAKUMAR.M at 6-Jun-11 2:51am
   
I tried this also but its not Focusing on that Textbox1 itself .
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v4
Comments
SARAVANAKUMAR.M at 6-Jun-11 2:56am
   
I tried this also but its not Focusing on that Textbox1 itself . i thnik it might be some problem ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

try to put : ClientIDMode="Static" on TextBox.
 
<asp:textbox id="Textbox1" runat="server" clientidmode="Static" xmlns:asp="#unknown"></asp:textbox>
 
Good Luck Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

U need to debug the javascript code for that. Thereafter, u'll know where's the problem.
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 28 Nov 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100