Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jade checkbox checked attribute unchecked based on conditional (if)

How do I get jade to render the checked attribute of a checkbox based on a conditional? Like these two versions of the HTML checkbox tag:

This seems to be the ONLY valid version of unchecked:

>  <input type="checkbox" name="vehicle" value="Bike">

While this is checked:

> <input type="checkbox" name="vehicle" value="Car" checked="checked">

Here is what I've tried so far:

This Jade is fine:

    input(type="checkbox", name="completed", checked=(true===true ? "checked" : "")).checkbox

because it renders this:

<input type="checkbox" name="completed" checked="checked" class="checkbox">

but this Jade is not fine:

    input(type="checkbox", name="completed", checked=(false===true ? "checked" : "")).checkbox

because it renders this:

<input type="checkbox" name="completed" checked="" class="checkbox">

instead of this:

<input type="checkbox" name="completed" class="checkbox">

How do I get Jade to render the entire checked attribute instead of just the value of the checked attibute?

like image 284
jstevens13 Avatar asked Jan 03 '13 21:01

jstevens13


2 Answers

You can use:

input(type="checkbox", name="completed", checked=(true===false ? "checked" : undefined))
like image 169
Michael Yin Avatar answered Oct 24 '22 18:10

Michael Yin


No need to specify the values:

input(type="checkbox", name="completed", checked=(condition))

If condition is false, there is no checked attribute will added.

like image 44
Vladimir Kalinichev Avatar answered Oct 24 '22 18:10

Vladimir Kalinichev