Reference createRadio()

createRadio()

Creates a radio button element.

The parameter is optional. If a string is passed, as in let myRadio = createSelect('food'), then each radio option will have "food" as its name parameter: <input name="food">. If an existing

or <span></span> element is passed, as in let myRadio = createSelect(container), it will become the radio button's parent element.

Radio buttons extend the p5.Element class with a few helpful methods for managing options:

  • myRadio.option(value, [label]) adds an option to the menu. The first parameter, value, is a string that sets the option's value and label. The second parameter, label, is optional. If provided, it sets the label displayed for the value. If an option with value already exists, its label is changed and its value is returned.
  • myRadio.value() returns the currently-selected option's value.
  • myRadio.selected() returns the currently-selected option.
  • myRadio.selected(value) selects the given option and returns it as an HTMLInputElement.
  • myRadio.disable(shouldDisable) enables the entire radio button if true is passed and disables it if false is passed.

Examples

Syntax

createRadio([containerElement])
createRadio([name])
createRadio()

Parameters

containerElement
Object:

container HTML Element, either a <div></div> or <span></span>.

name
String:

name parameter assigned to each option's <input></input> element.

Returns

p5.Element: new p5.Element object.
Notice any errors or typos? Please let us know. Please feel free to edit src/dom/dom.js and open a pull request!

Related References