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
')?
views:
510answers:
3
+8
A:
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;
});
});
Jose Basilio
2009-04-30 14:27:22
Is there anyway to do this for the inputs within a given fieldset or div tag?
AlteredConcept
2009-04-30 14:30:36
Yes, just use add the id of the given fieldset or div in the selector using #id.
Jose Basilio
2009-04-30 14:41:20
Thanks jose. I'll try this and get back to you if i have any questions
AlteredConcept
2009-04-30 14:47:04
+2
A:
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;
});
});
RedWolves
2009-04-30 14:31:04
You can replace the fieldset or div with the ID or class of them to get specific fieldsets or divs.
RedWolves
2009-04-30 14:32:17
+2
A:
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
});
farinspace
2009-04-30 14:35:36
good article on JS event delegation ... http://www.sitepoint.com/blogs/2008/07/23/javascript-event-delegation-is-easier-than-you-think/
farinspace
2009-05-05 01:42:12