Im trying to catch the textbox element by javascript, in order to put text into it. so how to set id to the textbox ??
<tr> // Id to this textbox <td>@Html.TextBoxFor(item => item.OperationNo)</td> </tr>
and then to put text into it by JS
// document.getElementById("Textbox id").Text= " Some text " ;
By default, the TextBoxFor element will have an ID and NAME property that matches the expression property of the element. If you want to specify an ID or NAME that's different from the expression property, you can use the htmlAttributes overload param. Thanks!
Just add the id property to the html-attributes. That will override the default id generated by the editorfor-helper-methode.
The TextBox for the Name value is created using Html. TextBoxFor function while the TextBox for the Mobile Number value is created using Html. TextBox helper function. The MaxLength of both the TextBoxes is set using the HTML MaxLength attribute using the HtmlAttributes parameter in Html.
IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.
You can set ID of a textbox like this.
@Html.TextBoxFor(item => item.OperationNo, new { id = "MyId" })
OR
@Html.TextBoxFor(item => item.OperationNo, htmlAttributes: new { id = "MyId" })
<input ... id="MyId" name="OperationNo" type="text" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With