How to keep date format based on its category?
Issues that I notice:
- (minor) You're binding to the click event of the radio button, rather than the change event. Be aware that this won't work for someone using a keyboard to fill out the form.
- Whenever the radio button changes, you're showing/hiding all of the day fields on the page.
- Reusing values for your
id
attributes. Those are supposed to be unique on the page. I don't think this is your issue, but it can cause weird bugs. Switch toclass
.
Here's what I would recommend.
- Wrap each "stat" on the form in a div with a class that indicates if it's averaged or instance. i.e.
<div class="stat averaged">
. - Modify your .erb to output the right class names on the div (so it's correct when the page first loads).
- Modify your javascript to change the class name of the parent div (
$(this).closest('.stat').removeClass('averaged').addClass('instance');
) - Setup css rules to hide the day field for averaged stats (
.stat.averaged .day { display: none; }
)