How can the cursor be focus on a specific input box on page load?
Is it posible to retain initial text value as well and place cursor at end of input?
<input type="text" size="25" id="myinputbox" class="input-text" name="input2" value = "initial text" />
This is what works fine for me:
fff will be a global variable which name is absolutely irrelevant and which aim will be to stop the generic onload event to force focus in that input.
From: http://webreflection.blogspot.com.br/2009/06/inputfocus-something-really-annoying.html
Just a heads up - you can now do this with HTML5 without JavaScript for browsers that support it:
You probably want to start with this and build onto it with JavaScript to provide a fallback for older browsers.
There are two parts to your question.
1) How to focus an input on page load?
You can just add the
autofocus
attribute to the input.However, this might not be supported in all browsers, so we can use javascript.
2) How to place cursor at the end of the input text?
Here's a non-jQuery solution with some borrowed code from another SO answer.
Here's an example of how I would accomplish this with jQuery.
Working fine...
A portable way of doing this is using a custom function (to handle browser differences) like this one.
Then setup a handler for the
onload
at the end of your<body>
tag, as jessegavin wrote:If you can't add to the BODY tag for some reason, you can add this AFTER the Form: