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

The selected function for radio buttons created with createRadio does not work when making multiple changes to which option is selected. #5603

Closed
1 of 17 tasks
sflanker opened this issue Feb 16, 2022 · 1 comment · Fixed by #5604
Labels

Comments

@sflanker
Copy link
Contributor

Most appropriate sub-area of p5.js?

  • Accessibility (Web Accessibility)
  • Build tools and processes
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Friendly error system
  • Image
  • IO (Input/Output)
  • Localization
  • Math
  • Unit Testing
  • Typography
  • Utilities
  • WebGL
  • Other (specify if possible)

p5.js version

1.4.1

Web browser and version

Chromium 98, Safari 15.3, FireFox 96

Operating System

MacOS

Steps to reproduce this

Steps:

  1. Create a radio button with createRadio
  2. Add multiple options
  3. Cycle through those options repeatedly using the selected(value) function

Expected: you should be able to cycle through the options repeatedly

Actual: the radio buttons get stuck on the last option

Snippet:

const options = ["foo", "bar", "baz"];

function setup() {
  noCanvas();
  noLoop();

  let radio = createRadio();
  options.forEach((v) => radio.option(v));
  radio.selected("foo");

  let ix = 0;
  let nextButton = createButton("Next");
  nextButton.mouseClicked(() => {
    const opt = options[++ix % options.length];
    radio.selected(opt);
  });

  let fixButton = createButton("Fix");
  fixButton.mouseClicked(() => {
    // override radio.selected with a version that works properly
    let self = radio;
    // Here's the fix:
    self.selected = function(value) {
      let result = null;
      if (value === undefined) {
        for (const option of self._getOptionsArray()) {
          if (option.checked) {
            result = option;
            break;
          }
        }
      } else {
        // deselect all first (Google Chrome wigs out when multiple options have the checked attribute set to true)
        self._getOptionsArray().forEach(option => option.removeAttribute("checked"));
        for (const option of self._getOptionsArray()) {
          if (option.value === value) {
            option.setAttribute("checked", true);
            result = option;
            break;
          }
        }
      }
      return result;
    };
  });
}

function draw() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant