0

I have an input

<input type='text' id='email'/>

For some event I'd like to focus on that textbox

$('#email').click()

But this code doesn't work for some reasons (Chrome). How can I achieve my goal?

5
  • focus() perhaps? Commented Dec 10, 2016 at 18:04
  • focus doesn't work as well.. Commented Dec 10, 2016 at 18:04
  • jsfiddle.net/64chq1jr Seems to work. Commented Dec 10, 2016 at 18:08
  • Do you have any other elements with the id of email? If so, then they might conflict with each other - ids are meant to be unique. Is the code executed after the element has been loaded? It would also be useful if you posted more of your script... Commented Dec 10, 2016 at 18:08
  • some event can you define it? Commented Dec 10, 2016 at 18:14

1 Answer 1

1

Instead of click you should use focus() to have a focus on the desired target element and make sure to wrap it inside doc ready block:

$(function(){ // <-------doc ready
   $('#email').focus(); // <----use focus event
});

Document ready ensures that DOM is ready and can be used. Either you wrap your js code inside this block or put the script at the bottom of the page. which runs when whole page gets loaded.


If you are using HTML5 then you might look into autofocus attribute:

<input type='text' id='email' autofocus />

Sign up to request clarification or add additional context in comments.

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.