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

Omit the value attribute from select options with no value #3773

Merged
merged 3 commits into from
Jun 9, 2023

Commits on Jun 9, 2023

  1. Omit value attribute from options with no value

    In HTML, if you omit the `value` attribute from an `<option>`the value defaults to the text contained inside the element [1].
    
    However if you omit the value from an option when using the `govukSelect` macro, we currently include an empty value attribute on the `<option>` element. This would mean that when submitting a form using a select with options with no value set, the value submitted would be the empty string.
    
    Instead, omit the `value` attribute if no `value` option has been provided. We then mimic the default HTML behaviour, as the option value will then naturally fall back to the text content.
    
    We still need to ensure that the `value` attribute is included when the value option is passed but has a falsey value (like an empty string, 0, or boolean false).
    
    Fixes #3440.
    
    [1]: https://html.spec.whatwg.org/multipage/form-elements.html#the-option-element:~:text=The%20value%20attribute%20provides%20a%20value%20for%20element.%20The%20value%20of%20an%20option%20element%20is%20the%20value%20of%20the%20value%20content%20attribute%2C%20if%20there%20is%20one%2C%20or%2C%20if%20there%20is%20not%2C%20the%20value%20of%20the%20element%27s%20text%20IDL%20attribute.
    36degrees committed Jun 9, 2023
    Configuration menu
    Copy the full SHA
    72b9c71 View commit details
    Browse the repository at this point in the history
  2. Allow selecting options based on implicit value

    Allow the top-level `value` option on a `govukSelect` to work with options that have no explicit value set.
    
    The value of an option element when there is no `value` attribute is the text content of the element, so use that instead.
    36degrees committed Jun 9, 2023
    Configuration menu
    Copy the full SHA
    e1bab47 View commit details
    Browse the repository at this point in the history
  3. Document in CHANGELOG

    36degrees committed Jun 9, 2023
    Configuration menu
    Copy the full SHA
    d8222a6 View commit details
    Browse the repository at this point in the history