p5.Element class() Method Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The class() method of p5.Element in p5.js is used to set or return the classes of the element. When no class is specified as a parameter, it returns the current classes of the element. An element can have multiple classes assigned to it. Also, one class can be specified to multiple elements on the page.Syntax:class(class)Parameters: This function accepts a single parameter as mentioned above and described belowclass: It is a string that denotes the class of the element.Example: The example below illustrates the class() method in p5.js. JavaScript function setup() { createCanvas(550, 300); textSize(18); text("Click on the button to add the given " + "class(es) to the element", 20, 20); setBtn = createButton("Create new Element with given classes"); setBtn.position(30, 40); setBtn.mouseClicked(createNewElement); setBtn = createButton("Show Last Element class"); setBtn.position(300, 40); setBtn.mouseClicked(showClasses); class_name = createInput('tmpClass'); class_name.position(30, 80); } function createNewElement() { clear(); // Create a new p5.Element tmpElement = createElement("div"); // Get the class to set let classToSet = class_name.value(); // Set the class of the element tmpElement.class(classToSet); text("Class set with the names: " + classToSet, 30, 120); text("Click on the button to add the given " + "class(es) to the element", 20, 20); } function showClasses() { clear(); // Get the classes of the element let setClasses = tmpElement.class(); // Display the classes text("The classes of the element are: " + setClasses, 30, 120); text("Click on the button to add the given " + "class(es) to the element", 20, 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.Element/class 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