Reputation: 339
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
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
Reputation: 31
I had same problem, this resolved my problem without any delay
setTimeout(function() {
$('#goal-input').focus();
});
Upvotes: 0
Reputation: 4177
This will work
$('input').click(function() {
$(this).focus();
})
Upvotes: 10
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
Reputation: 3610
This will work:
$('input[type="text"]').click(function() {
$(this).focus();
})
Upvotes: 0
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
Reputation: 4273
Use Document.ready
Try this..
$(function(){
$("#txtDate").focus();
});
Upvotes: 3