Reputation: 207
I have a very simple one page website that has a input field in it. I want to make the input field focused when the window/site is loaded so that the user/visitor can start typing on it right away the site is loaded!
I want it NOT IN JQUERY. IF POSSIBLE PLEASE GIVE ME A SOLUTION IN JAVASCRIPT PLEASE.
Thanks in advance.
Upvotes: 1
Views: 426
Reputation:
HTML5 has autofocus attribute for this job. You could check from here if your target browser(s) has support for this attribute.
Upvotes: 0
Reputation: 14039
There is a new input field attribute call autofocus
. Supported by all browsers except IE
http://www.html5tutorial.info/html5-autofocus.php
You can use that and provide fallback with the solutions others are providing
var i = document.createElement('input');
if(!('autofocus' in i)) {
window.onload=function(){
document.getElementById('ID').focus();
});
}
Upvotes: 1
Reputation: 146191
window.onload=function(){
document.getElementById('ID').focus();
});
Upvotes: 4
Reputation: 6689
Add it to the onLoad callback so that it will focus after the page has been loaded:
<body onLoad="function() { document.getElementById('input_field_id').focus(); }">
Upvotes: 1
Reputation: 17478
You need to use focus()
method in javascript.
In body onload method, call the javascript method, where you need to set the focus on the input field.
<script type="text/javascript">
function load()
{
document.getElementById("inputFieldId").focus();
}
</script>
<body onload="load()">
Upvotes: 0
Reputation: 5291
//ID is the id of textbox
document.getElementById('ID').focus();
Upvotes: 2