Thursday, August 28, 2025
HomeLanguagesJavascriptp5.js remove() Function

p5.js remove() Function

The remove() function is an inbuilt function which is used to remove the element and deregister all listeners.

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




<script language="javascript"
    type="text/javascript" src="path/to/p5.dom.js">
</script>


Syntax:

remove()

Parameters: This function does not accepts any parameters.

Below examples illustrate the remove() function in p5.js:

Example 1: This example creates an element.




function setup() {  
     
    // Create Canvas of given size 
    var cvs = createCanvas(600, 250);
}
  
function draw() {
    
  // Set the background color
  background('green'); 
    
  // Use createDiv() function to
  // create a div element
  var myDiv = createDiv('Welcome to neveropen');
    
  // Set the position of div element
  myDiv.position(150, 100);  
    
  // Set the font-size of text
  myDiv.style('font-size', '24px');
    
  // Set the font color
  myDiv.style('color', 'white');
    
  // Comment the remove() function 
  // myDiv.remove();
}


Output:

Example 2: This example uses remove() function to remove the element.




function setup() {  
     
    // Create Canvas of given size 
    var cvs = createCanvas(600, 250);
}
  
function draw() {
    
  // Set the background color
  background('green'); 
    
  // Use createDiv() function to
  // create a div element
  var myDiv = createDiv('Welcome to neveropen');
    
  // Set the position of div element
  myDiv.position(150, 100);  
    
  // Set the font-size of text
  myDiv.style('font-size', '24px');
    
  // Set the font color
  myDiv.style('color', 'white');
    
  // Use remove() function to remove div element
  myDiv.remove();
}


Output:

RELATED ARTICLES

Most Popular

Dominic
32236 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6609 POSTS0 COMMENTS
Nicole Veronica
11779 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11828 POSTS0 COMMENTS
Shaida Kate Naidoo
6719 POSTS0 COMMENTS
Ted Musemwa
7002 POSTS0 COMMENTS
Thapelo Manthata
6678 POSTS0 COMMENTS
Umr Jansen
6690 POSTS0 COMMENTS