The value() function is an inbuilt function which is used to set or return the value of the element.
This function requires p5.dom library. So add the following line in the head section of the index.html file.
javascript
<script language= "javascript"
type= "text/javascript" src= "path/to/p5.dom.js" >
</script>
|
Syntax:
value()
or
value( value )
Parameters: This function accepts single parameter value which holds the value in number or string format.
Return Value: This function returns the value of the element.
Below examples illustrate the value() function in p5.js:
Example 1: This example uses value() function to display the value as output.
javascript
var input_val;
function setup() {
createCanvas(400, 200);
background( 'green' );
input_val = createInput( 'Welcome to GeeksforGeeks' );
input_val.position(30, 80);
input_val.style( 'width' , '250px' );
input_val.style( 'font-size' , '20px' );
input_val.style( 'margin-left' , '50px' );
}
function mousePressed() {
print(input_val.value());
}
|
Output:
Example 2: This example uses value() function to set the value of an element.
javascript
var input_val;
function setup() {
createCanvas(400, 200);
background( 'green' );
input_val = createInput( 'Welcome to GeeksforGeeks' );
input_val.position(30, 80);
input_val.style( 'width' , '250px' );
input_val.style( 'font-size' , '20px' );
input_val.style( 'margin-left' , '50px' );
}
function mousePressed() {
input_val.value( 'A computer science portal' );
}
|
Output:
- Before Click on the Element:
- After Click on the Element:
Last Updated :
21 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...