setText

Category:UI Controls

Sets the text for the specified screen element.

Your apps will sometimes need to change, or clear, the words displayed on screen elements. setText() can be used to update the text on a button, textInput, textLabel, or textArea.

Examples

Example: Click Counter

Update the number on a button with every click.

// Update the number on a button with every click
var count=0;
button("clickCounter","0");
onEvent("clickCounter", "click", function(event) {
  count=count+1;
  setText("clickCounter", count);
});

Example Random Thoughts

Demonstrate reading and then clearing a textInput box.

// Demonstrate reading and then clearing a textInput box.
textInput("yourThought","");
onEvent("yourThought", "change", function(event) {
  write(getText("yourThought"));
  setText("yourThought","");
});

// Create a label for a screen title.
textLabel("screenTitle","");
setText("screenTitle","My App");

Syntax

setText(id, text)

Parameters

Name Type Required? Description
text string The text displayed within the screen element.
id string The unique identifier for the screen element. Must begin with a letter, contain no spaces, and may contain letters, digits, - and _.

Returns

No return value. Modifies screen only.

Tips

  • To clear the text on a screen element set the text to be "". Make sure you getText() first if you need to save the data from a textInput() to a variable.

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