I would like to autofocus on an editorfor in my application, but I can't seem to do that. I have successfully used autofocus on a textbox, but I would like to use an editorfor to keep my application's look universal.
Any solutions to this would be much appreciated, thank you.
My attempt:
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" }, autofocus = "" })
You put the autofocus attribute in the wrong spot.
Try this instead:
I've been following this thread and I may have stumbled on an answer to your question about autofocus on EditorFor - this is all Asp.Net 4.5 and MVC 5, not that it matters.
In the Scripts folder I have a jQuery script file:
$(function(){ $('.someclassname').focus(); });
I add the script name to the BundleConfig and render it in the view.
<div class="col-md-10" someclassname">
type="text" autofocus="autofocus"
to the EditorFor's @class. So,new{@class="form-control", type="text", autofocus="autofocus"
.someclassname
field gets the cursor focus... PS. In fact if you just do (3) it works also...This s because you are using EditorFor instead of something specific like TextBoxFor.
Or you can do that using jQuery:
Update:
As you know
TextBoxFor
always creates a textbox with type input, ButEditorFor
is a little bit smart, it renders markup based on the datatype of the property.Using .Net Framework 4.5 and MVC 5, this works for me: