← All articles
development

jQuery Tip: Brute Force Dirty Form Detection

A quick jQuery hack to auto-enable a disabled save button the moment any input on the form changes -- simple, if not the most performant, approach.

Donn Felker

Donn Felker

2010.04.20 · 1 min read · updated July 5, 2013

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).

Donn Felker

Written by Donn Felker

I've spent 25+ years shipping software, writing books, and running my own companies. I write about thinking clearly, working for yourself, and doing real work while AI rewrites the rules. New essays land here every week.

Get the newsletter

Keep reading