@@ -13,6 +13,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}
1313 return
1414 }
1515
16+ const blankOptionText = 'Select none'
1617 const placeholderOption = this . module . querySelector (
1718 'option[value=""]:first-child'
1819 )
@@ -42,6 +43,21 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}
4243 const inner = this . containerInner . element
4344 const input = this . input . element
4445 inner . prepend ( input )
46+ } else {
47+ // Update text for blank option in the hidden select
48+ const selectElement = this . passedElement . element
49+ if ( selectElement . id . search ( 'blank' ) > 0 ) {
50+ selectElement . firstChild . innerText = blankOptionText
51+ }
52+ // Update text for blank option in the choices dropdown
53+ // choices.js 'lastChild' in this context is the listbox of choices:
54+ const listbox = this . dropdown . element . lastChild
55+ // choices.js 'firstChild' in this context is the first option.
56+ // This always displays "Select One" for selects with a blank option:
57+ var blankOption = listbox . firstChild
58+ if ( blankOption && blankOption . id . search ( 'blank' ) > 0 ) {
59+ blankOption . innerText = blankOptionText
60+ }
4561 }
4662 // Add aria-labelledby to the listbox as well as the combobox
4763 const listbox = this . itemList . element
@@ -54,6 +70,23 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}
5470 }
5571 } )
5672
73+ // Reset blank 'Select One' to 'Select None' on each change
74+ // This is because choices.js rebuilds the widget on each
75+ // change event and resets the text. We can't use the preferred
76+ // refresh method as this loses the current state of the dropdown.
77+ const selectElement = this . choices . passedElement . element
78+ const listbox = this . choices . dropdown . element . lastChild
79+ selectElement . addEventListener (
80+ 'change' ,
81+ function ( event ) {
82+ var blankOption = listbox . firstChild
83+ if ( blankOption && blankOption . id . search ( 'blank' ) > 0 ) {
84+ blankOption . innerText = blankOptionText
85+ }
86+ } ,
87+ false
88+ )
89+
5790 this . module . choices = this . choices
5891 }
5992
0 commit comments