If you use jQuery and don't insert your fields, you can make a simple binding that joins all your page controls within the fields, and whenever you focus / don't focus on any of these controls, the class is added / removed from The field set that contains the control.
Here is an example:
$('input, label, select, option, button', 'fieldset').each(function (index, item) { $(this).focus(function () { $(this).closest('fieldset').addClass('fieldsetFocus'); }); $(this).blur(function () { $(this).closest('fieldset').removeClass('fieldsetFocus'); }); });
source share