Donn Felker

Not A Subscriber?

Join thousands of people who build and refuse to get left behind. One message a week on building, using AI as an accelerator, and staying sharp in the new AI frontier.

Receive one free message a week

April 20, 2010 Donn Felker · updated July 5, 2013

jQuery Tip: Brute Force Dirty Form Detection

If you disable your “save” button on your MVC views when the page first loads and want them to be auto-enabled when any input on the form changes, you can do that with the following jQuery snippet.

This is sort of a hack*, but it works.

With this HTML

<input type="submit" id="myButton" value=" Save " disabled />

and this javascript:

<script>
$(function () {
$(":input").change(function () {
// if any input element changes, enable the save and undo buttons
$("#<%=myButton.ClientID %>").removeAttr('disabled');
});
});
</script>

You’ll “enable” the button when the form inputs are changed. This is kind of nice when you have a TON of inputs on the page and you need to be able to detect when ANY of them have changed.

* Why its a Hack: I’m not limiting the selector to an ‘id’ therefore sizzle scans the entire DOM which could be a bummer in regards to performance).