getChecked

Category:UI Controls

Gets the state of a checkbox or radioButton.

getChecked() is used to check whether a user has checked a checkbox or radio button by using the element ID. Check boxes are used when the user is asked to check a box if they agree with something, like an app privacy statement. Radio buttons are used when the user is asked to choose one item from a predefined group of options.

Examples

// Retrieve and display a checkbox value.
checkbox("agreeBox", false);
textLabel("agreeLabel","I agree to the above privacy statement.","agreeBox");

textLabel("response1","Response: ");
textLabel("response2","");
onEvent("agreeBox", "click", function() {
  setText("response2",getChecked("agreeBox"));
});

Example: Finding the Checked Item

Iterate over the radio buttons in a group to determine which one is selected each time the favorite button is clicked.

// Iterate over the radio buttons in a group to determine which one is selected each time the favorite button is clicked.
radioButton("Red", false,"ColorGroup");
textLabel("RedLabel","Red","Red");
radioButton("Blue", false,"ColorGroup");
textLabel("BlueLabel","Blue","Blue");
radioButton("Green", false,"ColorGroup");
textLabel("GreenLabel","Green","Green");
radioButton("Orange", false,"ColorGroup");
textLabel("OrangeLabel","Orange","Orange");

button("favorite","What's my favorite color?");
onEvent("favorite","click", function() {
    var radioIDs = ["Red","Blue","Green","Orange"];
    var index = 0;
    while (index < radioIDs.length && !getChecked(radioIDs[index])) {
      index++;
    }
    console.log("Your favorite color is: " + radioIDs[index]);
});

Syntax

getChecked(id)

Parameters

NameTypeRequired?Description
id string The unique identifier for the screen element. Must begin with a letter, contain no spaces, and may contain letters, digits, - and _.

Returns

Boolean true or false.

Tips

  • A checkbox or radio button can also be created in design mode.

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