What's the correct syntax for an HTML helper (in MVC2) to define an onblur handler where the textbox is generated with code like:
<%=Html.TextBox(
"ChooseOptions.AddCount" + order.ID,
(order.Count > 0) ? AddCount.ToString() : "",
new { @class = "{number: true} small-input" }
)
What's the correct syntax for an HTML helper (in MVC2) to define an onblur handler where the textbox is generated with code like:
<%=Html.TextBox(
"ChooseOptions.AddCount" + order.ID,
(order.Count > 0) ? AddCount.ToString() : "",
new { @class = "{number: true} small-input" }
)
Share
Improve this question
edited Jul 7, 2014 at 21:15
abatishchev
100k88 gold badges301 silver badges442 bronze badges
asked May 8, 2010 at 17:52
justStevejustSteve
5,52420 gold badges77 silver badges140 bronze badges
2 Answers
Reset to default 8Add the onblur
to htmlattributes
<%=Html.TextBox(
"ChooseOptions.AddCount" + order.ID,
(order.Count > 0) ? AddCount.ToString() : "",
new { @class = "{number: true} small-input", onblur = "alert('fired')" }
) %>
Or a better way add it with jQuery
$('#ChooseOptions_AddCount' + id).onblur(function() { alert('fired'); });
I wanted to submit a solution which allows you to reuse your function code, if you want. You can define the function elsewhere, and then call it from the HTML element.
Somewhere in the myView.cshtml file (maybe at the top of the body tags) you can define a script and define myFunc inside, like this:
<script>
function myFunc() {
alert('fired');
}
</script>
Then call it from the HTML element, like this:
<%=Html.TextBox(
"ChooseOptions.AddCount" + order.ID,
(order.Count > 0) ? AddCount.ToString() : "",
new { @class = "{number: true} small-input", onblur = "myFunc()" }
) %>
Or, if you're happy to use a HTML helper to define your textbox, you can do
@Html.TextBoxFor(m => m.ChooseOptions.AddCount,
(order.Count > 0) ? AddCount.ToString() : "",
new { @class = "{number: true} small-input", onblur = "myFunc()" }
)