button

Category:UI Controls

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.

Found a bug in the documentation? Let us know at documentation@code.org

Examples

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();
});

// Create a "Click Me" button.
button("id", "Click Me!");

Syntax

button(id, text)

Parameters

Name Type Required? Description
text string The text displayed within the button.
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 _.

Returns

No return value. Modifies screen only.

Tips

Found a bug in the documentation? Let us know at documentation@code.org