user2122825
user2122825

Reputation: 57

Autofocus on the textbox in html razor code

I want the cursor to be focused on the textbox when I load the page. I tried by adding autofocus="" but I couldn't find any difference in that.

Any help would be greatly appreciated!

@Html.TextBoxFor(
    m => m.UserName, 
    "@xyz.com", 
    new { @class = "form-control input-lg", @id = "username"})

Upvotes: 0

Views: 2686

Answers (4)

D Mishra
D Mishra

Reputation: 1578

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

Upvotes: 0

scottheckel
scottheckel

Reputation: 9244

The autofocus attribute isn't implemented in all browsers. It's a good subset of browsers with most newer browsers having it, but not all. It is likely you were using a browser that does not support the autofocus attribute. Dive into HTML 5 has a good listing of the browser support.

Upvotes: 0

RandomUser
RandomUser

Reputation: 1853

You could use plain java script like this:

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

Upvotes: 0

Nitin Varpe
Nitin Varpe

Reputation: 10694

If you are ok with jquery, you can try using

<script type="text/javascript">
    $(function () {
        $("#username").focus();
    });
</script>

Upvotes: 1

Related Questions