Reputation: 57
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
Reputation: 1578
<script type="text/javascript">
$(document).ready(function(){
$("#username").focus();
});
}); </script>
Upvotes: 0
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
Reputation: 1853
You could use plain java script like this:
<script type="text/javascript">
document.getElementById("username").focus();
</script>
Upvotes: 0
Reputation: 10694
If you are ok with jquery, you can try using
<script type="text/javascript">
$(function () {
$("#username").focus();
});
</script>
Upvotes: 1