Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Hi Friends ,

Can any one suggest me, to set focus on Texbox using Jquery , Im using the Coding
C#
$("input[id$='txtDate']").blur(function ()
      {
          if(!checkDate($("txtDate").val()))
          {
            alert("Invalid Date");
            $("#txtDate").val("");
            $('#txtDate').focus();
           }
      });

This is the Coding but in this , the value Textbox is not clearing its value And also its not set Focusing in that Textbox "txtDate" .
Posted

it's easy just do the following:
$('#yourTextBoxID').focus();

or you can make the selection by class name like
$('.yourTextBoxClass:first').focus();


but if you are using a server side textbox
i.e <asp:textbox id="txtUser" xmlns:asp="#unknown">
you should put the ID like this
$('#' + <%= txtUser.ClientID %>).focus();
 
Share this answer
 
Comments
Uday P.Singh 22-Jun-11 4:24am    
good one my 5 :)
SARAVANAKUMAR.M 22-Jun-11 5:14am    
Thanks Friends ,but still its not Focusing in that textbox itself.
RaviRanjanKr 22-Jun-11 6:37am    
Nice Answer, My 5 :)
bukagedi 26-Jan-12 6:50am    
This solution is clear, but working in .NET 4.x you can set ClientIDMode="Static" for every server side control. There is no need for using expression <%= txtUser.ClientID %> working in this case: clientId and serverSideID are equal in this case.
You can use like this

C#
var txtBox=document.getElementById("txtUserName" );
txtBox.focus();



Change txtUserName to your Asp.net textbox ClientID

Thanks
 
Share this answer
 
Comments
RaviRanjanKr 22-Jun-11 6:38am    
Nice Answer Tarun! have a my 5 :)
Uday P.Singh 22-Jun-11 15:45pm    
OP is asking question related to jquery not in native JavaScript. I think you misread the question.
[no name] 6-Mar-12 10:45am    
regardless this should work. theres no problem using this instead of jquery selector.
YOGESH NANDRE 19-Apr-13 6:23am    
:)
In addition to the answer of Mohamad4love try this to clear the textbox:

$("#<%=txtDate.ClientID%>").val('');


hope it helps :)
 
Share this answer
 
yes me also faced such an issue
So I played a little trick with JQuery to set the focus correctly

In normal case just putting the $("#yourtextbox").focus() should work, but still its not working properly, change ur code to

focus on some other control before the alert, and after alert, keep the focus back to the text box,

$("input[id$='txtDate']").blur(function ()
      {
          if(!checkDate($("txtDate").val()))
          {
            $("#someothercontrol").focus();
            alert("Invalid Date");
            $('#txtDate').focus();
            $("#txtDate").val("");
            
           }
      });


Check whether this will solve this issue eventhough this is not a right approach
 
Share this answer
 
Comments
SARAVANAKUMAR.M 22-Jun-11 7:32am    
I tried it but it set focusing in "#someothercontrol" textbox only not focusing to "#txtDate" textbox.
Shobhana.n 3-Mar-13 2:29am    
Yes It will not work.I have commented my ans,May be it will be helpful for you SARAVANAKUMAR.
can you update your HTML file.It ll very helpful to answer. actually blur is very powerful event in jquery, you might or may setting focus on several filed in your checkDate
method.
it loose focus while working with several field.

i dont kwon whether you by mistake written
JavaScript
if(!checkDate($("txtDate").val()))
, coz you have missed # before your textfield id..
$("#txtDate")



mean while you can try this.

JavaScript
$(function(){
alert(checkDate($("txtDate").val()));
//if alert will not show <big>true</big> then rest of code work

if(!checkDate($("#txtDate").val()))
          {
            $("#someothercontrol").focus();
            alert("Invalid Date");
            $("#txtDate").val("");
            //first clear the value
            $('#txtDate').focus();
            
           }

});



If you are using ajax or some server side validation,
it wont work you have to aply some little tricks like for ajax you have to synchronize ajax method before client side validation.

and you can also use setinterval or settimeout method of java script for setting focus. please ckeck it
C#
setTimeout(function() {
            $('#txtDate').focus();
        }, 0);




Thnks,
deepak
 
Share this answer
 
Hi,I think you are doing wrong ID selection.
Try bellow, code I replaced selector's selection.I think it will work.

$("input[id$='txtDate']").blur(function () {
var txtVal = $("input[id$='txtDate']").val();
if (!checkDate(txtVal)) {
alert("Invalid Date");
$("input[id$='txtDate']").val("");
$("input[id$='txtDate']").focus();
}
});
 
Share this answer
 
 
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