# Example of boolean input type in node html file?

**URL:** https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135
**Category:** General
**Created:** [4 August 2020 13:54 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135 "2020-08-04T13:54:02Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [4 August 2020 13:54 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/1 "2020-08-04T13:54:02Z")

</div>

Developing a node, where I need to have boolean value selected. I have seen this in a few nodes, and in various places in NR editor and such. But not sure where a 'good' example of implementing this is? I figure it is just a limited drop down list? Thanks.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [4 August 2020 14:06 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/2 "2020-08-04T14:06:44Z")

</div>

If you use the typed inputs feature then you can tell it you want a boolean value and it will happen automatically. [https://nodered.org/docs/api/ui/typedInput/](https://nodered.org/docs/api/ui/typedInput/)

---

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [4 August 2020 14:30 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/3 "2020-08-04T14:30:45Z")

</div>

What I need is just a simple user select-able true/false control in the node edit view.

I saw the documentation you reference above, read through it, but not seeing how the entire thing comes together. This...

```auto
{
    value:"bool",
    label:"boolean",
    icon:"red/images/typedInput/bool.png",
    options:["true","false"]
}

```

Seems like exactly what I am looking for, but how drop this into a typical node HTML file? The documentation states...

A replacement for a regular `<input>` that allows the type of the value to be chosen, including options for string, number and boolean types.

Ok, how does one do this? If I am starting from this, where power UI control should just be a simple select-able true or false setting...

```auto
  <div class="form-row">
      <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
      <input type="text" id="node-input-name" />
  </div>

  <div class="form-row">
      <label for="node-input-device"><i class="fa fa-desktop"></i> Device</label>
      <input type="text" id="node-input-device" />
  </div>

  <div class="form-row">
      <label for="node-input-power"><i class="fa fa-desktop"></i> Power</label>
      <input type= **????** id="node-input-power" />
  </div>
</script>

<script type="text/javascript">
  RED.nodes.registerType("MAX7219Power", {
    category: "MAX7219",
    color:"#E6E0F8",
    icon:'font-awesome/fa-desktop',
    defaults: {
      name: {value:"", required: false},
      device: {value:"0", required: true, validate: RED.validators.number()},
      **???** power: {value:true, required: true},
    },
    outputs:1,
    inputs:1,
    label: function() {
      return this.name ? this.name : "MAX7219 Power";
    },
    labelStyle: function() { return this.name ? "node_label_italic" : "";},
    outputLabels: ["Clear"],
    inputLabels: ["Trigger"]
  });
</script>

```

The \*\*??? \*\* notations are where I am wondering what do I add/change? I have not done much (input oriented) HTML, so I am sure that is tripping me up here as well.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [4 August 2020 14:46 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/4 "2020-08-04T14:46:08Z")

</div>

If you look at this node (not yet published to npm) [https://github.com/colinl/node-red-contrib-influxdb-backup](https://github.com/colinl/node-red-contrib-influxdb-backup) you will see how I use it. The unzip field is a true/false value that can passed in a message parameter, context, environment or directly in the config, so if you look for all references to unzip you will see how I do it.  
On the other hand if you only want it to be configurablle via the node then may be simpler to use a checkbox rather than a dropdown.

**[Edit]** What I usually do is to find a node that does what I want and then look at how that node does it.

---

<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: [4 August 2020 15:11 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/5 "2020-08-04T15:11:27Z")

</div>

I agree with @Colin, where it makes sense, use `typedInputs` so that users have a choice of setting it to `bool-true` or `bool false` or automating it by sending in the value into your node via `msg.payload.power` or `msg.what_ever_var_they_choose` or `flow.some_flow_variable_for_this_power_thingy` etc

It just makes things much more flexible for the user.

Of course you would default the `typedInput` to 'bool' type - then if the user needs more flexibility, they set it to `msg.xxxxx` themselves.

---

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [4 August 2020 15:32 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/6 "2020-08-04T15:32:34Z")

</div>

Ok, good to know... but to address my original question.... how to do I do this exactly? A complete example of how to do it, would be greatly appreciated. In this specific use case, no user flexibility is applicable, the input sure always be, simply, true or false, in all cases.

---

<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: [4 August 2020 16:09 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/7 "2020-08-04T16:09:18Z")

</div>

> [@Nodi.Rubrum](#):
>
> how to do I do this exactly

Well as Colin said, search his example for `unzip`

to give you a nudge - this is whats required...

- In your html file
  - in the `defaults`...
    - myBool: { value: false },
    - myBoolType: { value: "bool" },

  - in `oneditprepare()`...
    - `$("#node-input-myBool").typedInput({ default: 'bool', types: ['bool', 'msg', 'flow', 'global', 'env'], typeField: $("#node-input-myBoolType") }); `

  - in the html part (form-row)...
    - `<input type="text" id="node-input-myBool"> <input type="hidden" id="node-input-myBoolType">`

- In your js file...
  - At initialisation, near the top, just below `RED.nodes.createNode(this,config);`...
    - `node.myBool = config.myBool;`
    - `node.myBoolType = config.myBoolType;`

  - later on (usually inside `node.on("input", function(){ ...})` )...
    - `let myBool = RED.util.evaluateNodeProperty(node.myBool, node.myBoolType, node, msg)`
    - now use myBool as required in your logic (be prepared for users sending duff values though - so I recommend a sanity check on the value of `mybool` - perhaps even be a good citizen & call `node.error("power is not a boolean value", msg)` to inform the runtime of this error

Rinse and repeat as many for as many fields you want.

* * *

> [@Nodi.Rubrum](#):
>
> the input sure always be, simply, true or false

Umm... "it should always be true OR false"? - that is still a choice 😃 😃

The point of the `typedInput` is to permit the user to make that **choice** both inside your node (at design time in your UI) AND outside of it (by allowing the user/runtime to pass in `true` or `false` instead of "hard coding it").

That said, there are times it doesn't make sense to provide a choice (for example when a choice should never change once the node is initialized)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [4 August 2020 16:10 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/8 "2020-08-04T16:10:30Z")

</div>

If you only want to configure it via the node then just use a dropdown or a checkbox.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [4 August 2020 16:15 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/9 "2020-08-04T16:15:40Z")

</div>

> [@Steve-Mcl](#):
>
> In your html file
> 
> - in the `defaults` ...
> - myBool: { value: false },
> - myBoolType: { value: "bool" },

If the field can _only_ be a boolean, then the `myBoolType` is not needed.

So you can get rid of the `typeField` parameter and its hidden input, and hardcode the value `"bool"` in the call to `RED.util.evaluateNodeProperty` in place of `node.myBoolType`.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [4 August 2020 16:20 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/10 "2020-08-04T16:20:13Z")

</div>

But for a 'simple' boolean value, a checkbox may well be far more appropriate.

In which case, you can see how the Switch node does it for the 'repair message sequences' checkbox - [https://github.com/node-red/node-red/blob/master/packages/node\_modules/%40node-red/nodes/core/function/10-switch.html#L37](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/function/10-switch.html#L37)

in your `defaults`:

```auto
defaults: {
   myBoolean: { value:false}
}

```

and in your HTML template:

```auto
<div class="form-row">
        <input type="checkbox" id="node-input-myBoolean" style="display: inline-block; width: auto; vertical-align: top;">
        <label style="width: auto;" for="node-input-myBoolean">A label for the checkbox</label></input>
    </div>

```

---

<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: [4 August 2020 17:31 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/11 "2020-08-04T17:31:23Z")

</div>

> [@knolleary](#):
>
> If the field can _only_ be a boolean, then the `myBoolType` is not needed

Is that also true for when permitting `msg` and `flow` as an option (i.e. permitting the value to be determined at runtime)?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [4 August 2020 18:01 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/12 "2020-08-04T18:01:50Z")

</div>

It's true in any situation where there is only one type available in the typedInput, so there is little point storing that one hardcoded value in the node configuration.

---

<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: [4 August 2020 18:30 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/13 "2020-08-04T18:30:23Z")

</div>

But the example I posted had bool, msg, flow, global etc. How would the users selection be remembered? Some under the hood magic I guess?

Edit...  
To be clear, I'm just double checking my knowledge Nick (i don't wanna be posting bad advice)

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [4 August 2020 18:51 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/14 "2020-08-04T18:51:57Z")

</div>

Your example has multiple possible types, so yes the node needs some way of knowing which type has been selected.

All of my replies have been in the context of there only being one available type, so there's no choice to store.

---

<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: [4 August 2020 19:16 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/15 "2020-08-04T19:16:46Z")

</div>

Phew. Close one 😉

---

<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: [3 October 2020 19:16 UTC](https://discourse.nodered.org/t/example-of-boolean-input-type-in-node-html-file/31135/16 "2020-10-03T19:16:52Z")

</div>

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