App Lab Documentation

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.

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

NameTypeRequired?Description
idstring

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 _.

textstring

The text displayed within the button.

Returns

No return value. Modifies screen only.

Tips

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