I'm using ASP.NET 2.0 with a Master Page, and I was wondering if anyone knew of a way to detect when the fields within a certain <div>
or fieldset
have been changed (e.g., marked 'IsDirty
')?

- 30,738
- 21
- 105
- 131

- 2,602
- 7
- 32
- 36
10 Answers
You could bind the Change event for all inputs and flag a variable as true. Like this.
var somethingChanged = false;
$(document).ready(function() {
$('input').change(function() {
somethingChanged = true;
});
});
But, keep in mind that if the user changes something, then changes back to the original values, it will still be flagged as changed.
UPDATE: For a specific div or fieldset. Just use the id for the given fieldset or div. Example:
var somethingChanged = false;
$(document).ready(function() {
$('#myDiv input').change(function() {
somethingChanged = true;
});
});

- 50,714
- 13
- 121
- 117
-
Is there anyway to do this for the inputs within a given fieldset or div tag? – AlteredConcept Apr 30 '09 at 14:30
-
Yes, just use add the id of the given fieldset or div in the selector using #id. – Jose Basilio Apr 30 '09 at 14:41
-
Thanks jose. I'll try this and get back to you if i have any questions – AlteredConcept Apr 30 '09 at 14:47
-
1Be aware that this solution leads to an undesiderable behaviour when dealing with UI. See my answer please. – Luca Fagioli Oct 15 '12 at 09:38
-
change event has many downsides, see http://www.quirksmode.org/dom/events/change.html – reto Jan 18 '13 at 15:34
-
`change` won't get fired when the input value changes dinamically :( – David Gras Nov 26 '14 at 18:34
-
When the input is attached to a jquery Datepicker, the method is not fired when the user uses the datepicker to change the date, any help on that ? – Walllzzz Mar 16 '16 at 15:32
-
7I would highly recommend one replaces `$('input')` with `$(':input')` -- The colon will match all types of inputs rather than input tags. Eg: input, textarea, select. – Joshua Burns Jul 21 '16 at 18:04
-
Referring to this answer, is there any way we can handle it if the user does indeed change back to the original value? like `somethingChanged` will return to `false`? – Jun 15 '17 at 03:52
-
@user827391012 Yes there is, [check my answer](https://stackoverflow.com/a/11795226/636561). – Luca Fagioli Jul 28 '18 at 10:01
Quick (but very dirty) solution
This is quick, but it won't take care of ctrl+z
or cmd+z
and it will give you a false positive when pressing shift
, ctrl
or the tab
key:
$('#my-form').on('change keyup paste', ':input', function(e) {
// The form has been changed. Your code here.
});
Quick (less dirty) solution
This will prevent false positives for shift
, ctrl
or the tab
key, but it won't handle ctrl+z
or cmd+z
:
$('#my-form').on('change keyup paste', ':input', function(e) {
var keycode = e.which;
if (e.type === 'paste' || e.type === 'change' || (
(keycode === 46 || keycode === 8) || // delete & backspace
(keycode > 47 && keycode < 58) || // number keys
keycode == 32 || keycode == 13 || // spacebar & return key(s) (if you want to allow carriage returns)
(keycode > 64 && keycode < 91) || // letter keys
(keycode > 95 && keycode < 112) || // numpad keys
(keycode > 185 && keycode < 193) || // ;=,-./` (in order)
(keycode > 218 && keycode < 223))) { // [\]' (in order))
// The form has been changed. Your code here.
}
});
A complete solution
If you want to handle all the cases, you should use:
// init the form when the document is ready or when the form is populated after an ajax call
$(document).ready(function() {
$('#my-form').find(':input').each(function(index, value) {
$(this).data('val', $(this).val());
});
})
$('#my-form').on('change paste', ':input', function(e) {
$(this).data('val', $(this).val());
// The form has been changed. Your code here.
});
$('#my-form').on('keyup', ':input', function(e) {
if ($(this).val() != $(this).data('val')) {
$(this).data('val', $(this).val());
// The form has been changed. Your code here.
}
});

- 12,722
- 5
- 59
- 57
-
2The jquery method "live()" has been deprecated. You can use "on" instead. $('select').on('change', function...... – Alan B. Dee May 06 '14 at 18:27
-
1I edited the answer for a even better solution, based on the post http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements. Thanks a lot, Luca – Marcus Vinicius Pompeu Mar 15 '16 at 04:03
-
A simple and elegant solution (it detects form elements changes in real time):
var formChanged = false;
$('#my-div form').on('keyup change paste', 'input, select, textarea', function(){
formChanged = true;
});

- 30,738
- 21
- 105
- 131

- 8,841
- 9
- 64
- 79
-
3Love it. I replaced `'input, select, textarea'` with `:input` for simplicity though ;) – Joshua Burns Jul 21 '16 at 18:06
-
@Marcio Mazzucato May you tell me why do you need event 'keyup' over here? Thank you for your explanation. – Thomas.Benz Nov 12 '17 at 17:08
-
@Thomas.Benz, Sure! Because `.change()` event fires only when selector has lost focus. You can see more info [in this answer](https://stackoverflow.com/a/1443299/999820). – Marcio Mazzucato Nov 12 '17 at 18:59
-
@Marcio Mazzucato . Thank you for your explanation. By the way, I think your code can be made more simplified using selector ':input'. So 'input, select, textarea' can be replaced by ':input' as Joshua Burns mentioned. About :input Selector can be seen at http://api.jquery.com/input-selector/ – Thomas.Benz Nov 13 '17 at 12:55
-
@Marcio Mazzucato I have one more question: if for some text box or text area, if user only use the mouse to copy and paste text, it seems the detector method is not fired until the text box or text area loses focus. So how can deal with that situation? Maybe some mouse events are needed to as in the event selector besides 'keyup' and 'change'? – Thomas.Benz Nov 13 '17 at 13:31
-
1@Thomas.Benz, I am not using `:input` only for an easier readability, but you can implement it. For the case you mentioned, you can use the `paste` event, i updated the answer right now, thanks! – Marcio Mazzucato Nov 13 '17 at 20:14
-
1@Marcio Mazzucato Perfect. Now "the mouse copying and pasting data also making form input changes" is included. – Thomas.Benz Nov 19 '17 at 13:54
-
**Warning** This approach will give you a false positive simply by pressing `shift`, `ctrl`, the `tab` key or any other non-printable character, which is wrong. Check it with [this fiddle](https://jsfiddle.net/fu9vaxwL/61/). Check [my answer](https://stackoverflow.com/a/11795226/636561) for the solution. – Luca Fagioli Jul 28 '18 at 09:08
For a form you could serialize the contents on load then compare serialization at a later time, e.g.:
$(function(){
var initdata=$('form').serialize();
$('form').submit(function(e){
e.preventDefault();
var nowdata=$('form').serialize();
if(initdata==nowdata) console.log('nothing changed'); else console.log('something changed');
// save
initdata=nowdata;
$.post('settings.php',nowdata).done(function(){
console.log('saved');
});
});
});
Note this requires form elements to have a name
attribute.

- 1,495
- 13
- 15
-
2Thanks for this! Your solution is extremely elegant, simple and works with forms containing arrays of values. – Todd Hammer Jan 24 '19 at 14:44
Just to clarify because the question is "within a certain fieldset/div":
var somethingChanged = false;
$(document).ready(function() {
$('fieldset > input').change(function() {
somethingChanged = true;
});
});
or
var somethingChanged = false;
$(document).ready(function() {
$('div > input').change(function() {
somethingChanged = true;
});
});

- 10,379
- 12
- 49
- 68
-
You can replace the fieldset or div with the ID or class of them to get specific fieldsets or divs. – RedWolves Apr 30 '09 at 14:32
You can give the fieldset or div an ID and bind the change event to it ... the event should propagate from the inner children.
var somethingChanged = false;
$('#fieldset_id').change(function(e)
{
// e.target is the element which triggered the event
// console.log(e.target);
somethingChanged = true;
});
Additionally if you wanted to have a single event listening function you could put the change event on the form and then check which fieldset changed:
$('#form_id').change(function(e)
{
var changedFieldset = $(e.target).parents('fieldset');
// do stuff
});

- 8,422
- 6
- 33
- 46
-
good article on JS event delegation ... http://www.sitepoint.com/blogs/2008/07/23/javascript-event-delegation-is-easier-than-you-think/ – farinspace May 05 '09 at 01:42
-
That's the best solution. Thank you! Although it still ignores when input values change dinamically. – David Gras Nov 26 '14 at 18:31
I came up with this piece of code in CoffeeScript (not really field tested, yet):
Add class 'change_warning' to forms that should be watched for changes.
Add class 'change_allowed' to the save button.
change_warning.coffee:
window.ChangeWarning = {
save: ->
$(".change_warning").each (index,element) ->
$(element).data('serialized', $(element).serialize())
changed: (element) ->
$(element).serialize() != $(element).data('serialized')
changed_any: ->
$.makeArray($(".change_warning").map (index,element) -> ChangeWarning.changed(element)).some (f)->f
# AKA $(".change_warning").any (element) -> ChangeWarning.changed(element)
# But jQuery collections do not know the any/some method, yet (nor are they arrays)
change_allowed: ->
ChangeWarning.change_allowed_flag = true
beforeunload: ->
unless ChangeWarning.change_allowed_flag or not ChangeWarning.changed_any()
"You have unsaved changes"
}
$ ->
ChangeWarning.save()
$(".change_allowed").bind 'click', -> ChangeWarning.change_allowed()
$(window).bind 'beforeunload', -> ChangeWarning.beforeunload()
An alternative to Dw7's answer if you only want the fields inside a fieldset then you can call serialize() on its input values. Note: serialize() will not pickup any elements that do not have a "name" attribute. This will work for select tags as well.
var initialValues = $('#your-fieldset :input').serialize();
$('form').submit(function(e) {
e.preventDefault();
var currentValues = $('#your-fieldset :input').serialize();
if (currentValues == initialValues) {
// Nothing has changed
alert('Nothing was changed');
}
else {
this.submit();
}
});

- 41
- 4
.live
is now deprecated and replaced by .on
:
var confirmerSortie = false;
$(document).on('change', 'select', function() {
confirmerSortie = true;
});
$(document).on('change keypress', 'input', function() {
confirmerSortie = true;
});
$(document).on('change keypress', 'textarea', function() {
confirmerSortie = true;
});

- 30,738
- 21
- 105
- 131

- 261
- 4
- 11
The following solution worked for me:
$("#myDiv :input").change(function() { $("#myDiv").data("changed",true);});
}
if($("#myDiv").data("changed")) {
console.log('Form data changed hence proceed to submit');
}
else {
console.log('No change detected!');
}
Thanks