javascript - populating first field in form with cursor - Stack Overflow

I'm designing a series of forms. Is there a way I can mark a field to be populated by the browsers

I'm designing a series of forms. Is there a way I can mark a field to be populated by the browsers cursor (just like this pages loads with the cursor already in the title field above).

Thanks Giles

I'm designing a series of forms. Is there a way I can mark a field to be populated by the browsers cursor (just like this pages loads with the cursor already in the title field above).

Thanks Giles

Share Improve this question edited Nov 27, 2010 at 15:57 shamittomar 46.7k12 gold badges76 silver badges80 bronze badges asked Nov 27, 2010 at 15:53 gilesgiles 111 silver badge2 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

Use focus() function like this in Javascript. Put this code at the end of your HTML code, just before the </body> tag:

<script language="javascript" type="text/javascript">

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

</script>

Replace the textboxid with the ID of the <input> text box you are using in your form.

You can use Javascript to set your desired input focus:

Using JavaScript:

<script language="javascript" type="text/javascript">
    document.getElementById("xtpo_1").focus();
</script>

Using a JavaScript Library like Jquery:

<script language="javascript" type="text/javascript">
    $(document).ready(function() {
        $("#xtpo_1").focus();
    });
</script>

Even better, write an function

function autoFocus(x){

 document.getElementById(x).focus();

}

and put it in a separate javascript file called basic_functions.js or something.

Load that file at the top of your page in the header like this

<script type="text/javascript" src="basic_functions.js"></script>

In your body tag, call the function onload.

<body onload="focus('first_name')" >

Then create you input:

<input type='text' value='' name='first_name' id='first_name' />

That way you keep javascript functions out of your page and you can reuse the function on other pages. you can then put other basic functions like this in that file and include it in all pages where it is needed

In a HTML5 page, for browser supporting HTML5, you can do:

<input name="lorem" autofocus>

Vanilla Javascript:

<input id="lorem" name="lorem">
...
<script language="javascript" type="text/javascript">
  document.getElementById("lorem").focus();
</script>
</body>
</html>

jQuery:

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

Also you can do it in jQuery style and paste js-code anywhere:

HTML:

<input type="text" id="name"><br />
<input type="text" id="email">

Javascript:

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

Check it online: http://www.jsfiddle/4d5JG/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745201356a4616342.html

相关推荐

  • javascript - populating first field in form with cursor - Stack Overflow

    I'm designing a series of forms. Is there a way I can mark a field to be populated by the browsers

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信