Furkan Gözükara
Furkan Gözükara

Reputation: 23830

How set cursor inside textbox after page load with jquery

Alright i have a asp.net textbox and i want to set cursor inside textbox after page loaded like when you open google.

I only need set cursor inside textbox code.

I tried this but not working

$('txtSendTo').focus();

Upvotes: 19

Views: 49700

Answers (2)

Usman Shaukat
Usman Shaukat

Reputation: 1331

I was not able to set cursor inside an input field when a link is clicked. However adding event.preventDefault() at the start of the function and returning false fixed it. Here is the code if someone is having the same issue

$("#search-button").click(function (event) {
  event.preventDefault();
  $("#textbox").focus();
  return false;
});

Upvotes: 0

Jason Gennaro
Jason Gennaro

Reputation: 34855

If txtSendTo is an id, you need a #

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


If txtSendTo is a class, you need a .

$('.txtSendTo').focus();


Or, if there is only one textbox on the page

$('textbox').focus();


Also make sure the page is fully loaded before you try to search the dom:

$(document).ready(function () {
  `$('textbox').focus();`
});

Upvotes: 37

Related Questions