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.
// 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")); });
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]); });
getChecked(id)
Name | Type | Required? | Description |
---|---|---|---|
id | string | The unique identifier for the screen element. Must begin with a letter, contain no spaces, and may contain letters, digits, - and _. |
Boolean true or false.
Found a bug in the documentation? Let us know at documentation@code.org