1

I have an HTML helper textBox like this:

@Html.TextBox("txt1") <br />

Now I want to fire a javascript onchange event on this textbox. Is it possible to do so or should I use an HTML input type instead.

2 Answers 2

1

You can specify html attributes using the htmlAttributes parameter of the TextBox Html helper method like so:

@Html.TextBox("txt1", null, new { onchange="..." })

See: InputExtensions.TextBox Method

From the above link:

The htmlAttributes parameter consists of an object that contains name/value pairs. The attributes that are specified in the name/value pairs depend on the HTML element that is being rendered.

Sign up to request clarification or add additional context in comments.

Comments

0

If you have an ID for this textbox, you can hook the event to the textbox using Jquery.

$("#textboxid").keydown(function(event) {
    alert("Hey!");
});

Note that I've used the Keydown event. You can also use press or up events.

Or you can make the binding in the ready event.

$(document).ready(function(){
        $("#textboxid").bind("onchange", yourfunction);
          });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.