Friday, August 29, 2025
HomeLanguagesJavascriptp5.js createSelect() Function

p5.js createSelect() Function

The createSelect() function in p5.js is used to create a dropdown menu element in the DOM (Document Object Model) for taking input. The .value() method is used to get the selected option. This function includes the p5.dom library. Add the following syntax in the head section.

Note: This function requires the p5.dom library. So add the following line in the head section of the index.html file.

<script src=”https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js”> 
</script>

Syntax:  

createSelect(multiple)

or  

createSelect(existing)

Parameters:  

  • multiple: It holds the state of input. Multiple if True, single if False.
  • existing: It holds the object DOM select element.

Return Value: This function returns the value of an attribute in p5.Element format.

Example: This example uses a dropdown menu to change the background color from the given options. 

Javascript




// Create a variable for dropdown menu object
var dropdown;
  
function setup() {
    // Create a canvas
    createCanvas(400,400);
    // Create a dropdown menu object 
    dropdown = createSelect();
    // Position the dropdown menu
    dropdown.position(150,200);
    // Set options
    dropdown.option("orange");
    dropdown.option("green");
    dropdown.option("skyblue");
}
  
function draw() {
    // Set the background-color as chosen 
    // from the dropdown menu
    background(dropdown.value());
}


Output: 

  • Before changing the color:

  • During changing the color:

  • After changing the color:

Online editor: https://editor.p5js.org/ 
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

Reference: https://p5js.org/reference/#/p5/createSelect
 

RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11790 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7012 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS