# How to get values of node configuration checkboxes

**URL:** <https://discourse.nodered.org/t/how-to-get-values-of-node-configuration-checkboxes/62202>\
**Category:** Developing Nodes\
**Created:** [4 May 2022 22:46 UTC](https://discourse.nodered.org/t/how-to-get-values-of-node-configuration-checkboxes/62202 "2022-05-04T22:46:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cookies](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@cookies](https://discourse.nodered.org/u/cookies)\
**Post date:** [4 May 2022 22:46 UTC](https://discourse.nodered.org/t/how-to-get-values-of-node-configuration-checkboxes/62202/1 "2022-05-04T22:46:16Z")

</div>

I'm creating a custom node, and for my UI I want to have a group of checkboxes for the user to select. When a checkbox is selected/deselected, I want this to toggle an array index corresponding to the checkbox's value, in the custom node's JS file to either 1 or 0. My issue is, I cannot figure out how to transmit the state of the checkboxes from the UI.

I've tried the method outlined here: [Node properties : Node-RED](https://nodered.org/docs/creating-nodes/properties), but it doesn't seem to be updating the properties. Ideally, I would pass a JSON object as a config property, containing information on whether each checkbox is toggled on/off. I would appreciate any pointers on how to go about this.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [5 May 2022 06:35 UTC](https://discourse.nodered.org/t/how-to-get-values-of-node-configuration-checkboxes/62202/2 "2022-05-05T06:35:53Z")

</div>

Hi @cookies - welcome to the forums!

Not sure I follow.

Given the below property definition

```auto
defaults: {

    myCheckBox: {
        value: false
    }
}

```

And the following node config UI component

```auto
<div class="form-row">
	    <label for="node-input-myCheckBox" style="width:130px"><i class="fa fa-pencil"></i> myCheckBox </label>
	    <input type="checkbox" id="node-input-myCheckBox">
	</div>

```

it will stay in sync, so when opening up the node properties the checkbox will will restore their correct state.

you can then obtain these properties in your module via

```auto
function Init(config) {
    RED.nodes.createNode(this, config);
    config.myCheckBox
     ....
}

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 May 2022 06:38 UTC](https://discourse.nodered.org/t/how-to-get-values-of-node-configuration-checkboxes/62202/3 "2022-05-05T06:38:27Z")

</div>

What @marcus-j-davies says 👆

Alternatively,  
use `oneditsave` callback to read the checkboxes and store them in `this.checkboxes` array. NOTE: `checkboxes` would need to be defined in the defaults .

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [5 May 2022 08:32 UTC](https://discourse.nodered.org/t/how-to-get-values-of-node-configuration-checkboxes/62202/4 "2022-05-05T08:32:54Z")

</div>

To echo the answer by @Steve-Mcl - the below should cater for it.

The definition and necessary callbacks.

```auto
RED.nodes.registerType('my-module', {
    defaults: {
        bits: {
            value: []
        }
    },
    oneditsave: Save,
    oneditprepare: Restore

});

function Save() {
    const NumCheckBoxes = 3;
    this.bits = [];
    for (let i = 0; i < NumCheckBoxes; i++) {
        this.bits.push($(`#CheckBoxIndex_${i}`).is(":checked"));
    }
}

function Restore() {
    for (let i = 0; i < this.bits.length; i++) {
        $(`#CheckBoxIndex_${i}`).prop('checked', this.bits[i]);
    }
}

```

And the HTML. No need for the `node-input-` ID prefix, as they are not directly associated with a config definition.

```auto
<div class="form-row">
    <label for="CheckBoxIndex_0" style="width:130px"><i class="fa fa-pencil"></i> CheckBox 1</label>
    <input type="checkbox" id="CheckBoxIndex_0">
</div>

<div class="form-row">
    <label for="CheckBoxIndex_1" style="width:130px"><i class="fa fa-pencil"></i> CheckBox 2</label>
    <input type="checkbox" id="CheckBoxIndex_1">
</div>

<div class="form-row">
    <label for="CheckBoxIndex_2" style="width:130px"><i class="fa fa-pencil"></i> CheckBox 3</label>
    <input type="checkbox" id="CheckBoxIndex_2">
</div>

```

Then, in your module  
`config.bits[0]` will yield `true` | `false` for checkbox 1

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 July 2022 08:33 UTC](https://discourse.nodered.org/t/how-to-get-values-of-node-configuration-checkboxes/62202/5 "2022-07-04T08:33:53Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
