textLabel
Creates a text label on the screen displaying the text provided and referenced by the given id at default location (0,0).
Your apps will sometimes need titles on a screen, or words next to other UI elements like radio buttons, check boxes, text inputs, and dropdown lists. If you want the text label to also trigger the events with a different UI element, you can reference that id using the optional third parameter forId.
Examples
Example: Label for a Text Input Box
Create a label and associate it with a text input box.
// Create a label and associate it with a text input box.
textLabel("YourNameLabel","Enter your name:", "YourName");
textInput("YourName","");
// Create a label for a screen title.
textLabel("screenTitle","My App");
Example Opinion Survey
Demonstrate a label for each of the user input screen elements. No event handlers have been defined yet.
// Demonstrate a label for each of the user input screen elements. No event handlers have been defined yet.
textLabel("textInputLabel","Name:", "textInputName");
textInput("textInputName","");
write("<br>");
textLabel("dropdownLabel","Year In School","dropdownYear");
dropdown("dropdownYear","Freshman","Sophomore","Junior","Senior");
write("<br>");
textLabel("radioTitleLabel", "Gender");
radioButton("radioFemale","false","genderGroup");
textLabel("radioFemaleLabel","Female","radioFemale");
radioButton("radioMale","false","genderGroup");
textLabel("radioMaleLabel","Male","radioMale");
write("<br>");
textLabel("checkBoxLabel","Excited about programming?", "checkBoxProgramming");
checkbox("checkBoxProgramming",false);
Syntax
textLabel(id, text, forId)
Parameters
Name | Type | Required? | Description |
---|---|---|---|
id | string | The unique identifier for the text label. The id is used for referencing the text label 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 text label. | |
forId | string | The id of the other UI element to associate the label with. |
Returns
Tips
- If there is another UI element at location (0,0) the text label 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()
. - You should always provide a label for your text input, radio button, check box, and drop down controls
- Text labels can also be created and initialized in Design mode.
Found a bug in the documentation? Let us know at support@code.org.