14

I want to set focus to the textbox when i click the text box. I tried the below code but am not getting as expected .

$('#txtDate').focus();

Thanks in advance.

2
  • can you share the html for these fields and the event handlers Commented Aug 26, 2013 at 5:11
  • Here is my script and html .. <script type="text/javascript"> $(function () { $('#txt').keypad(); var txtBox=document.getElementById("txt" ); txtBox.focus(); }); </script> </head> <body> <h1> Keyboard</h1> <input type="text" id="txt" > <input type="text" id="txte" > <input type="text" id="txtf" > <input type="text" id="txtg" > </body> </html> Commented Aug 26, 2013 at 5:31

7 Answers 7

10

This will work

$('input').click(function() {
  $(this).focus();
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I tried the above code its not working .. I have developed a app for tab . When i focus to textbox and then again wen i focus back to previous text box its not focussing
3

Demo

Use Document.ready Try this..

 $(function(){
    $("#txtDate").focus();
  });

Comments

2

write it using jQuery function as

$(function() {
  $("#txtDate").focus();
});

or if it also doesn't work then try

$(function() {
  $(document.getElementById("txtDate")).focus();
});

Comments

0

Write the code in document ready function

$(document).on('ready', (function () {
  $('#txtDate').focus();
 }));

It should work..

or you can write as in document ready

document.getElementById("txtDate").focus();

Comments

0

This will work:

$('input[type="text"]').click(function() {
  $(this).focus();
})

Comments

0

Your question is not completely clear. But the following answer might make sense to you. And write a CSS as below.

JS------
$('input').focus(function() 
    $(this).addClass("focus");
});

CSS----
.focus {
    border: 2px solid #AA88FF;//This you can set any color you want
    background-color: #FFEEAA;//This you can set any color you want
}

Comments

0

I had same problem, this resolved my problem without any delay

setTimeout(function() {
  $('#goal-input').focus();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.