Skip to content

Action to toggle the state of a checkbox #35794

@anrghg

Description

@anrghg

Description

Instead of adding or removing a class, the checked state of the referenced element is toggled, or set to a predefined boolean if the optional force argument is provided.

The target usage are HTML toggles for CSS rules: A checkbox controls the styles in an adjacent sibling div. Best practice, though not mainstream due to JS prevalence.

We need the toggleChecked() action as using <label> would prevent propagating the click to an enclosing anchor, and conversely we cannot check a box when tapping on a nested anchor element.

Alternatives Considered

The toggleClass() action as a workaround fails when JS is turned off. The checkbox cannot be nested in <noscript> as this disrupts the adjacent sibling combinator. Also, noscript elements are typically repurposed in AMP environments to provide a fallback experience regardless whether JS is actually turned off.

Additional Context

https://wordpress.org/plugins/anrghg/

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions