setNumber

Category:UI Controls

Sets the number for the specified screen element.

Your apps will sometimes need to change, or clear, the numbers displayed on screen elements*. setNumber() can be used to update the number on a textInput, textLabel, button or slider screen element.

*The slider design element was contributed by Mike and Mitchell Schmidt.

Examples

Example: Click Counter

Update the number on a button with every click.

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

textLabel("randomNumber","");
setNumber("randomNumber",randomNumber(0,100));

Example: Where is the mouse click?

Demonstrate setting the number on a slider. Assumes sliders named xPositionSlider and yPositionSlider have been placed on the screen in design mode.

// Demonstrate setting the number on a slider. Assumes sliders named xPositionSlider and yPositionSlider have been placed on the screen in design mode.
onEvent("screen1", "click", function(event) {
  setNumber("xPositionSlider",event.offsetX);
  setNumber("yPositionSlider",event.offsetY);
});

Syntax

setNumber(id, number)

Parameters

Name Type Required? Description
number number The number 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

  • You cannot use setText() with sliders which require a number to be set.

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