Default a radio option to checked

I've used createRadio() in my setup() to create a series of radio buttons with three options. Can I specify one of the radio buttons being checked by default? Is this something I can do in the js or in the html?

Answers

  • edited February 2018 Answer ✓

    You can do it in your setup().

    void setup(){
      radio = createRadio();
      radio.option('apple', 1);
      radio.option('bread', 2);
      radio.option('juice', 3);
      radio._getInputChildrenArray()[1].checked = true;
    }
    

    You are supposed to be able to pass a parameter to the .value() function to set a given radio button as checked too (according to the p5.js source, at least), but I couldn't get it to work!

    Like, this should work:

    radio.value(2);
    

    But doesn't...?

    https://github.com/processing/p5.js/blob/5e1aa5d28371f9bc07ee1d4930d33a04d4bd2481/lib/addons/p5.dom.js#L760

  • TFGuy44,

    This is nuts, but I have the exact opposite results from you. Your setup() example doesn't work for me, but passing a parameter into the .value() does work. Wha...?

    Regardless, you have solved my issue. TY TY TY.

  • HRMMRMRM... Okay!

    To be fair, I was just trying it using the EDIT button on this page:

    https://p5js.org/reference/#/p5/createRadio

    But since you got it working, no worries!

Sign In or Register to comment.