Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buttons added after domready don't get button JS behaviour bound to them #906

Closed
andymantell opened this issue Nov 30, 2023 · 0 comments · Fixed by #907
Closed

Buttons added after domready don't get button JS behaviour bound to them #906

andymantell opened this issue Nov 30, 2023 · 0 comments · Fixed by #907

Comments

@andymantell
Copy link
Contributor

andymantell commented Nov 30, 2023

Bug Report

What is the issue?

On page load, the button component's javascript binds to all elements with data-module="button" in order to run the debouncing, and the spacebar activation for links as buttons.

There's a comment in the code which says:

/**
  * Initialise an event listener for keydown at document level
  * this will help listening for later inserted elements with a role="button"
  */

but this isn't true. The button code is invoked with individual button elements as follows:

export default () => {
  const buttons = document.querySelectorAll('[data-module="nhsuk-button"]')
  buttons.forEach((el) => {
    new Button(el).init()
  })
}

which means that $module inside the code corresponds to an individual button, not to the document element. And so the events are bound directly to the individual buttons.

This means that any buttons which are added after this JS has run will not have this behaviour.

I suspect the comment in the JS came across from govuk-frontend when it was ported over, but during refactoring to make it follow nhsuk-frontend JS patterns the bug has been introduced.

I think the fix should be as simple as removing that querySelectorAll / foreach combo, and just init the Button class on the document element.

What was the environment where this issue occurred?

  • NHS.UK frontend package version: Anything 6.2.0 - 8.x has this issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant