Skip to content

Commit

Permalink
click label instead of input
Browse files Browse the repository at this point in the history
  • Loading branch information
owenatgov committed Oct 4, 2024
1 parent d411a91 commit cee7848
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ export class FileUpload extends GOVUKFrontendComponent {
locale: closestAttributeValue($input, 'lang')
})

this.$label = document.querySelector(`[for="${$input.id}"]`)

if (!this.$label) {
throw new ElementError({
componentName: 'File upload',
identifier: 'No label'
})
}

$input.addEventListener('change', this.onChange.bind(this))
this.$input = $input

Expand Down Expand Up @@ -148,7 +157,9 @@ export class FileUpload extends GOVUKFrontendComponent {
* When the button is clicked, emulate clicking the actual, hidden file input
*/
onClick() {
this.$input.click()
if (this.$label instanceof HTMLElement) {
this.$label.click()
}
}

/**
Expand Down

0 comments on commit cee7848

Please sign in to comment.