Bernard
Bernard

Reputation: 73

How do I GET (read) the value of a checkbox using Jade and Node.js

I am usingJade and Node.js to build a form, in which I have a checkbox. The checkbox is initially displayed checked/unchecked depending on the value of the variable ToCompute read from the server. I want to read the new value of the checkbox if the user modifies it. My code works if ToCompute is unchecked - then I am able to detect that the user checked the checkbox (req.body.ToCompute == on) But the other way does not work. More specifically, req.body does not contain any "ToCompute" entry if the user unchecks the checkbox.

Here is the Jade code

form(action=save_url, method="post", id="form")
    tr
        td  Compute value
        td  input(type="checkbox", name="ToCompute", checked=ToCompute, form="form")

Upvotes: 1

Views: 2339

Answers (1)

PrestonP
PrestonP

Reputation: 126

If req.body.ToCompute is undefined, you could default it to false.

var ToCompute = req.body.ToCompute || false;

Upvotes: 1

Related Questions