Skip to content

Commit

Permalink
Use delegated event handlers for checkboxes
Browse files Browse the repository at this point in the history
Rather than setting up an event handler on every individual checkbox, set up delegated event handlers on the wrapper.

This will significantly reduce the number of event handlers being bound on pages with large numbers of checkboxes (like finder-frontend).
  • Loading branch information
36degrees committed Feb 26, 2019
1 parent 14aca54 commit 3c9b802
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
var _this = this;
this.applyAriaControlsAttributes(scope);

$(scope).find('[data-nested=true] input[type=checkbox]').on('change', function(e) {
$(scope).on('change', '[data-nested=true] input[type=checkbox]', function(e) {
var checkbox = e.target;
var isNested = $(checkbox).closest('.govuk-checkboxes--nested');
var hasNested = $('.govuk-checkboxes--nested[data-parent=' + checkbox.id + ']');
Expand All @@ -23,7 +23,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
}
});

$(scope).find('input[type=checkbox]').on('change', function(e) {
$(scope).on('change', 'input[type=checkbox]', function(e) {
if (GOVUK.analytics && GOVUK.analytics.trackEvent) {
var $checkbox = $(e.target);
var category = $checkbox.data("track-category");
Expand Down

0 comments on commit 3c9b802

Please sign in to comment.