Focus Input Box On Load

JavascriptHtmlDomXhtml

Javascript Problem Overview


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" />

Javascript Solutions


Solution 1 - Javascript

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.

<input id="myinputbox" type="text" autofocus>

However, this might not be supported in all browsers, so we can use javascript.

window.onload = function() {
  var input = document.getElementById("myinputbox").focus();
}

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.

function placeCursorAtEnd() {
  if (this.setSelectionRange) {
    // Double the length because Opera is inconsistent about 
    // whether a carriage return is one character or two.
    var len = this.value.length * 2;
    this.setSelectionRange(len, len);
  } else {
    // This might work for browsers without setSelectionRange support.
    this.value = this.value;
  }

  if (this.nodeName === "TEXTAREA") {
    // This will scroll a textarea to the bottom if needed
    this.scrollTop = 999999;
  }
};

window.onload = function() {
  var input = document.getElementById("myinputbox");

  if (obj.addEventListener) {
    obj.addEventListener("focus", placeCursorAtEnd, false);
  } else if (obj.attachEvent) {
    obj.attachEvent('onfocus', placeCursorAtEnd);
  }

  input.focus();
}

Here's an example of how I would accomplish this with jQuery.

<input type="text" autofocus>

<script>
$(function() {
  $("[autofocus]").on("focus", function() {
    if (this.setSelectionRange) {
      var len = this.value.length * 2;
      this.setSelectionRange(len, len);
    } else {
      this.value = this.value;
    }
    this.scrollTop = 999999;
  }).focus();
});
</script>

Solution 2 - Javascript

Just a heads up - you can now do this with HTML5 without JavaScript for browsers that support it:

<input type="text" autofocus>

You probably want to start with this and build onto it with JavaScript to provide a fallback for older browsers.

Solution 3 - Javascript

$(document).ready(function() {
    $('#id').focus();
});

Solution 4 - Javascript

function focusOnMyInputBox(){                                 
    document.getElementById("myinputbox").focus();
}

<body onLoad="focusOnMyInputBox();">

<input type="text"  size="25" id="myinputbox" class="input-text" name="input2" onfocus="this.value = this.value;" value = "initial text">

Solution 5 - Javascript

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:

window.onload = function() {
  document.getElementById("myinputbox").focus();
}

Solution 6 - Javascript

Working fine...

window.onload = function() {
  var input = document.getElementById("myinputbox").focus();
}

Solution 7 - Javascript

Try:

Javascript Pure:

[elem][n].style.visibility='visible';
[elem][n].focus();

Jquery:

[elem].filter(':visible').focus();

Solution 8 - Javascript

very simple one line solution:

<body onLoad="document.getElementById('myinputbox').focus();">

Solution 9 - Javascript

This is what works fine for me:

<form name="f" action="/search">
    <input name="q" onfocus="fff=1" />
</form>

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.

<body onload="if(!this.fff)document.f.q.focus();">
    <!-- ... the rest of the page ... -->
</body>

From: http://webreflection.blogspot.com.br/2009/06/inputfocus-something-really-annoying.html

Solution 10 - Javascript

If you can't add to the BODY tag for some reason, you can add this AFTER the Form:

<SCRIPT type="text/javascript">
    document.yourFormName.yourFieldName.focus();
</SCRIPT>

Solution 11 - Javascript

Add this to the top of your js

var input = $('#myinputbox');
   
input.focus();

Or to html

<script>
    var input = $('#myinputbox');
   
    input.focus();
</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCodex73View Question on Stackoverflow
Solution 1 - JavascriptjessegavinView Answer on Stackoverflow
Solution 2 - JavascriptDavid CalhounView Answer on Stackoverflow
Solution 3 - JavascriptNasruddinView Answer on Stackoverflow
Solution 4 - JavascriptcrnlxView Answer on Stackoverflow
Solution 5 - JavascriptCamilo Díaz RepkaView Answer on Stackoverflow
Solution 6 - JavascriptMohammed JavedView Answer on Stackoverflow
Solution 7 - JavascriptKingRiderView Answer on Stackoverflow
Solution 8 - JavascriptkaviView Answer on Stackoverflow
Solution 9 - JavascriptRogerView Answer on Stackoverflow
Solution 10 - JavascriptZardiwView Answer on Stackoverflow
Solution 11 - JavascriptMartyView Answer on Stackoverflow