p5.js changed() Function Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The changed() function is fired whenever the value of an element gets changed. It can be used to detect changes in checkbox elements or select elements. It can also be used to attach an event listener to an element. Syntax: changed(fxn) Parameters: This function accepts a single parameter as mentioned above and described below. fxn: This is the callback function that would be called whenever a change is detected. It can be passed 'false', which would prevent the previous firing function to stop firing. Below examples illustrates the changed() function in p5.js: Example 1: Detecting changes in a checkbox element javascript let red = 0; let green = 0; let blue = 0; function setup() { createCanvas(600, 300); // create input boxes redCheckbox = createCheckbox('Red', false); redCheckbox.position(20, 40) redCheckbox.changed(redChanged); greenCheckbox = createCheckbox('Green', false); greenCheckbox.position(100, 40) greenCheckbox.changed(greenChanged); blueCheckbox = createCheckbox('Blue', false); blueCheckbox.position(180, 40) blueCheckbox.changed(blueChanged); } function draw() { clear() // change the fill color based // on current rgb the values fill(red, green, blue); rect(20, 80, 300, 300); textSize(20); text("Check the boxes to change the fill color", 10, 20); } // functions for each of the colors function redChanged() { if (this.checked()) red = 128; else red = 0; } function greenChanged() { if (this.checked()) green = 128; else green = 0; } function blueChanged() { if (this.checked()) blue = 128; else blue = 0; } Output: Example 2: Detecting changes in a select element javascript let red = 0; let green = 0; let blue = 0; function setup() { createCanvas(350, 300); textSize(18) text("Select the color to change the background color", 10, 20); // create select element selectElem = createSelect(); selectElem.position(20, 40); selectElem.option('Slecet'); selectElem.option('Red'); selectElem.option('Green'); selectElem.option('Blue'); selectElem.changed(changeColor); } function changeColor() { clear(); colorVal = this.value(); if (colorVal == "Red") { background("red"); } else if (colorVal == "Green") { background("green"); } else if (colorVal == "Blue") { background("blue"); } else background(128); text("Select the color to change the background color", 10, 20); } Output: Online editor: https://editor.p5js.org/ Environment Setup: https://www.geeksforgeeks.org/javascript/p5-js-soundfile-object-installation-and-methods/ Reference: https://p5js.org/reference/#/p5/changed Create Quiz Comment S sayantanm19 Follow 0 Improve S sayantanm19 Follow 0 Improve Article Tags : JavaScript Web Technologies JavaScript-p5.js Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like