Alex
Alex

Reputation: 43

return a checkbox value in jade. using node.js ,express and Mongoose

I saved the checkbox value in mongoDB. passed the value to the page by

if (req.session.user.Single === undefined) {single_status= 'false'} else {single_status= ''};

res.render('user-profile', { 
    Single: single_status,
)};

Then i have this in jade

.col-md-4
 label-option(for="checkbox")
 input(type="checkbox", checked="#{Single}", name='single', id='single')

on the rendered view. It appears checked="false" so it is always checked. how to a return the valve to the checkbox to present the existing checked / unchecked?

<label-option for="checkbox">
 <input type="checkbox" checked="false" name="single" id="single"> Single Status
</label-option> 

Upvotes: 1

Views: 1344

Answers (1)

Roco CTZ
Roco CTZ

Reputation: 1117

The checkbox HTML element only cares whether you mention the checked attribute or not. So, when you say checked="false" is the same as saying checked or checked="true".

If you want to uncheck the input element, you have to completely omit the checked attribute.

Example:

input(type ='radio', name='status', checked)

One way to do this dynamically is this one (source):

input(type="checkbox", name="status", checked=(true===false ? "checked" : undefined))

Doing it like this means that the checked attribute will be rendered if the expression is true; if the expression is false, it will simply omit it.

Upvotes: 1

Related Questions