Sujeeth
Sujeeth

Reputation: 339

How to set focus when there are many textbox using jquery?

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.

Upvotes: 14

Views: 54087

Answers (7)

Umm E Habiba Siddiqui
Umm E Habiba Siddiqui

Reputation: 594

write it using jQuery function as

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

or if it also doesn't work then try

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

Upvotes: 1

veeresh k
veeresh k

Reputation: 31

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

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

Upvotes: 0

Anil kumar
Anil kumar

Reputation: 4177

This will work

$('input').click(function() {
  $(this).focus();
})

Upvotes: 10

Umesh
Umesh

Reputation: 11

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
}

Upvotes: 0

Somnath Kharat
Somnath Kharat

Reputation: 3610

This will work:

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

Upvotes: 0

abc123
abc123

Reputation: 262

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();

Upvotes: 0

Prabhakaran Parthipan
Prabhakaran Parthipan

Reputation: 4273

Demo

Use Document.ready Try this..

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

Upvotes: 3

Related Questions