Click here to Skip to main content
15,887,468 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Can any one suggest me , How to Set Focus on same Textbox using Javascript in Asp.net.

if(document.getElementById("Textbox1").value=="")
    {
        alert ( 'Invalid Date' );
        document.getElementById('Textbox1').value="";
        document.getElementById('Textbox1').focus();
    }


The Above coding is the Javascript for "Textbox1" which i used in my Form . This coding is working Properly in INTERNER EXPLORER and in GOOGLE CHROME also 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 same Textbox1 itself When i use FIREFOX .
Posted

1 solution

Add some delay in focusing of the field. Something like this:
<br />
setTimeout(function(){document.getElementById('Textbox1').focus()}, 10);<br />


Also make sure that "document.getElementById" is returning you the required element.

Hope this helps!
 
Share this answer
 
Comments
SARAVANAKUMAR.M 23-Jun-11 3:30am    
Thanks RakeshMeena , but the popup message is coming twice when i press Enter button on that texbox1.
RakeshMeena 23-Jun-11 3:42am    
Which popup? I believe query was for setting focus only! Can you please share the complete javascript function and also mention that on which event you are calling that function!
SARAVANAKUMAR.M 23-Jun-11 4:02am    
Focus is working Properly , The above query which u have given is working Fine when i Press TAB BUTTON but the Alert Message which i mentioned in my script is displaying twice when i press ENTER BUTTON .
RakeshMeena 23-Jun-11 5:45am    
Got it! If you can also share the complete javascript function and the event on which you are calling that function (as I asked earlier as well).

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