FahimMurshed
FahimMurshed

Reputation: 207

How to make a input field focused after the site is loaded?

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

Answers (7)

user452425
user452425

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

sv_in
sv_in

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

The Alpha
The Alpha

Reputation: 146191

window.onload=function(){
    document.getElementById('ID').focus();
});

Upvotes: 4

Trevor
Trevor

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

Ilanchezhian
Ilanchezhian

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

Vinod
Vinod

Reputation: 4882

document.getElementById("ControlID").focus();

Upvotes: 1

Arvind Bhardwaj
Arvind Bhardwaj

Reputation: 5291

//ID is the id of textbox

document.getElementById('ID').focus();

Upvotes: 2

Related Questions