Side navigation
#11043 closed enhancement (plugin)
Opened December 16, 2011 03:27PM UTC
Closed December 16, 2011 04:26PM UTC
Form elements affected by a "reset" do not have their events triggered
Reported by: | e.sand@elisand.com | Owned by: | |
---|---|---|---|
Priority: | undecided | Milestone: | None |
Component: | unfiled | Version: | 1.7.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
When a form has the "reset" event fired, the browser resets all input (and textboxes) to their default (attr()) values. However, none of the click/change events that may be bound to those elements are triggered.
When a form reset event is fired, a "bubble down" should happen to trigger any events bound to input and textbox elements so that the form is truly reset.
... or is there reason to specifically not have this functionality built in to jQuery?
Attachments (0)
Change History (3)
Changed December 16, 2011 03:37PM UTC by comment:1
Changed December 16, 2011 04:19PM UTC by comment:2
... and here is a relatively simple jQuery code fix (written for 1.4.x, hence the lack of .attr() and .prop() and the use of .live()):
$('form').live('reset', function() { $(this).find('input, select, textarea').each(function() { if ($(this).is('input[type="radio"], input[type="checkbox"]')) { if ($(this).is(':checked') !== $(this)[0].defaultChecked) { $(this).triggerHandler('change'); } } else { if ($(this).val() !== $(this)[0].defaultValue) { $(this).triggerHandler('change'); } } }); });
Please note though that I'm only triggering the change event, not the click event. I would suppose that both events should trigger...
Also, I'm sure there can be clean-ups done in that code (e.g., my use of "$(this)" when "this" is already a jQuery object).
Changed December 16, 2011 04:26PM UTC by comment:3
resolution: | → plugin |
---|---|
status: | new → closed |
When a native "reset" event fires, the native "change" events do not fire so I think we are doing right default thing. You could certainly create a plugin with that code for people who wanted to do it. Be aware that the "reset" event does not bubble in all browsers, however, so you'll need to bind it directly to the form.
Simple test case: