Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptp5.js | html() Function

p5.js | html() Function

The html() function is used to set the inner HTML of the element by replacing any existing html. If the value of the second parameter is true then html is appended instead of replacing the existing html element. If this function does not contain any parameters then it returns the inner HTML of the element.

Note: This function requires the 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:

html()

or

html( html, append )

Parameters:

  • html: This parameter holds the HTML element in string format which needs to be placed inside the element.
  • append: This parameter holds the Boolean value to append existing HTML element.

Return Value: This function returns a string which contains the inner HTML of the element.

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

Example 1:




function setup() {
      
    // Create a canvas of given size
    createCanvas(400, 200);
      
    // Set background color
    background('green');
      
    var div = createDiv('');
      
    // Use html() function
    div.html('Welcome to neveropen');   
    
    // Set the position of div element
    div.position(60, 80); 
    
    // Set font-size of text
    div.style('font-size', '24px');
    
    // Set font-color of text
    div.style('color', 'white');
  


Output:

Example 2:




function setup() {
      
    // Create canvas of given size
    createCanvas(400, 200);
      
    // Set background color
    background('green');
      
    var div = createDiv('').size(200, 70);
  
    // Use html() function    
    div.html('Welcome to neveropen', true);   
    
    // Set the position of div element
    div.position(100, 60); 
    
    // Set font-size of text
    div.style('font-size', '24px');
    
    // Set font-color of text
    div.style('color', 'white');
    
    div.style('border', '1px solid white');
    
    div.style('text-align', 'center');
  


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments