How do you automatically set the focus to a textbox when a web page loads?
If you're using jquery:
$(function() {
$("#Box1").focus();
});
or prototype:
Event.observe(window, 'load', function() {
$("Box1").focus();
});
or plain javascript:
window.onload = function() {
document.getElementById("Box1").focus();
};
though keep in mind that this will replace other on load handlers, so look up addLoadEvent() in google for a safe way to append onload handlers rather than replacing.
Autofocus With Cursor On End Of Textbox
Please use this code and run in your browser
$(document).ready(function() { var input = $("#test"); var len = input.val().length; input[0].focus(); input[0].setSelectionRange(len, len);
});
<input type="text" id="test" autofocus value="value text" />
Focus Input Box On Load
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>
Force cursor to input box immediately after webpage is loaded
You can also use simply autofocus html5 attribute.But it is not supported in Internet Explorer 9 and earlier versions.
How to focus on a form input text field on page load using jQuery?
Set focus on the first text field:
$("input:text:visible:first").focus();
This also does the first text field, but you can change the [0] to another index:
$('input[@type="text"]')[0].focus();
Or, you can use the ID:
$("#someTextBox").focus();
Fill an Input without selecting it before. HTML / JS?
This should work:
$('#inputresponse').focus();
Just setting this on your page will automatically focus on the input element and anything typed will be added within.
Related Topics
Jquery-Ui Datepicker CSS Problem
Start Div Scrollbar from Bottom with Pure CSS
CSS Background Image Centered with Stellar Js
Jw Player: Cross-Browser "Display:None" Player Behavior
CSS Non-Standard "Zoom" Property
Rotate Text and Shrink Its Container to the New Width
I Want to Change the Select Icon/Dropdown Icon to (Fa-Chevron-Down). How Can I
Percentage Height in Nested Flex Box
How to Write A:Visited in Inline CSS
Stop Link's :Before Content from Being Underlined by Rule Applied to the Link
Centering a Single Column Using Bootstrap 4 Grid System
IE8 Renders Font Weights Randomly
How to Replace The Expand Icon (▶) of The <Details> Element