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?


  • 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:


    But doesn't...?

  • 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:

    But since you got it working, no worries!

Sign In or Register to comment.