Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET jQuery
Hi Friends ,
 
Can any one suggest me, to set focus on Texbox using Jquery , Im using the Coding
$("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 21-Jun-11 23:04pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
  Permalink  
Comments
Uday P.Singh at 22-Jun-11 4:24am
   
good one my 5 :)
SARAVANAKUMAR.M at 22-Jun-11 5:14am
   
Thanks Friends ,but still its not Focusing in that textbox itself.
RaviRanjankr at 22-Jun-11 6:37am
   
Nice Answer, My 5 :)
bukagedi at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can use like this
 
var txtBox=document.getElementById("txtUserName" );
txtBox.focus();
 

Change txtUserName to your Asp.net textbox ClientID
 
Thanks
  Permalink  
Comments
RaviRanjankr at 22-Jun-11 6:38am
   
Nice Answer Tarun! have a my 5 :)
Uday P.Singh at 22-Jun-11 15:45pm
   
OP is asking question related to jquery not in native JavaScript. I think you misread the question.
JeremyKyle at 6-Mar-12 10:45am
   
regardless this should work. theres no problem using this instead of jquery selector.
YOGESH NANDRE at 19-Apr-13 6:23am
   
:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In addition to the answer of Mohamad4love try this to clear the textbox:
 
$("#<%=txtDate.ClientID%>").val('');
 
hope it helps Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Comments
SARAVANAKUMAR.M at 22-Jun-11 7:32am
   
I tried it but it set focusing in "#someothercontrol" textbox only not focusing to "#txtDate" textbox.
Shobhana.n at 3-Mar-13 2:29am
   
Yes It will not work.I have commented my ans,May be it will be helpful for you SARAVANAKUMAR.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
if(!checkDate($("txtDate").val()))
, coz you have missed # before your textfield id..
$("#txtDate")
 

 
mean while you can try this.
 
$(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
setTimeout(function() {
            $('#txtDate').focus();
        }, 0);
 

 
Thnks,
deepak
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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();
}
});
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

<body ></body>
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 375
1 Sergey Alexandrovich Kryukov 329
2 CPallini 260
3 DamithSL 214
4 Maciej Los 197
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 3 Mar 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