openui / openui/open-ui

[checkbox/switch] unchecked value behaviour

Open
#313 17 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

checkbox WHATWG
Dominant language
MDX
Stars
4.5k
Forks
226
Avg merge
2d 22h
Merged PRs (30d)
4

Description

Discussion Summary

Currently checkboxes (and switches) have an odd behaviour around their value property and the forms submission behaviour.

Take <input type="checkbox" name="a"> this element will reflect .value as "on", but when a parent form is submitted it won't include any entry for "a".

As the checkbox is unchecked it's quite common to want false submitted when unchecked and true when checked. You can do the latter part by adding value="true" but when unchecked it still won't be included in the form submission (and .value will still return "true".)

There's workarounds using JS where you can listen for the formdata event and manually add the entry when unchecked, but it would be nice if we had a HTML solution to this.

Possibly a new attribute (name tbd) such as uncheckedvalue="false" which includes the input in the form submission automatically and perhaps updates the value getter to return the uncheckedvalue attribute's value when unchecked.

Original comment

I would really like it that when a checkbox is unchecked its value is an empty string.

I don't know about others, but i have to do this hack all the time:

let value = el.value; // other input
if (el.type==='checkbox') { // checkboxes
    value = el.checked ? el.value : '';
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the unchecked and checked behavior, including its .value and parent form submission, and review the formdata event workaround described in the issue. Done means an agreed HTML behavior and attribute design that specifies unchecked values, checked values, the value getter, and form submission semantics.

Written by the indexing model from the issue text.

Assessment

Tech stack
html, javascript
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.