My problem is a bit more complicated than this, but essentially when the user changes something on the page I would like to mark that field as "dirty" in some way and use a confirm box to ensure the user actually wants to leave the page before losing changes.
I can't use a global "something changed" variable in this case because of the need to add/remove it on certain items based on other events that occurred.
The answer to the question in the subject should take multiple things into account:
- General best practices - if such a thing exists for this stuff
- Lookup speed - I've seen in many places that class names are faster
- Browser compatibility - no super-fancy HTML5/CSS3 whizzbang features, please. I need this solution to work in IE7+, Firefox, Chrome, and Safari. Not too worried about versions on the non-IE browsers.
Basically, I have some jQuery code that looks like this:
$(":input").on("change", function(event) {
// set something on $(this) to hold state for this item
// $(this).addClass("my-dirty-flag");
// or
// this.setAttribute("data-dirty-flag", "data-dirty-flag");
});
Which option is "better" and why?
One thing that made me even consider asking this is that AngularJS seems to use properties on tags to do this sort of thing.