button
Creates a button on the screen displaying the text provided and referenced by the given id at default location (0,0).
Many apps use buttons to allow the user to initiate some app action. An event handler must be created for each type of user interaction with the button using onEvent()
and the id.
Examples
Example: Click Me Button
// Create a "Click Me" button.
button("id", "Click Me!");
Example: Simple Turtle Control 1
Move the turtle forward on every click of the button.
// Move the turtle forward on every click of the button.
button("forward", "Move Forward");
onEvent("forward", "click", function(event) {
moveForward();
});
Example: Simple Turtle Control 2
Move the turtle forward or backward depending on the button clicked.
// Move the turtle forward or backward depending on the button clicked.
button("forward", "Move Forward");
button("backward", "Move Backward");
onEvent("forward", "click", function(event) {
moveForward();
});
onEvent("backward", "click", function(event) {
moveBackward();
});
Syntax
button(id, text)
Parameters
Name | Type | Required? | Description |
---|---|---|---|
id | string | The unique identifier for the button. The id is used for referencing the button in event handlers or other UI element modification functions. Must begin with a letter, contain no spaces, and may contain letters, digits, - and _. | |
text | string | The text displayed within the button. |
Returns
Tips
- If there is another UI element at location (0,0) the button is placed at the next available position to the right or below.
- There are various UI element modification functions available:
setText()
,showElement()
,hideElement()
,deleteElement()
,setPosition()
,setSize()
. - There are various UI element query functions available:
getText()
,getXPosition()
,getYPosition()
. - Buttons can also be created and initialized in Design mode.
Found a bug in the documentation? Let us know at support@code.org.