Reputation: 23
I want to populate a html form using data passed through the variable 'object'. The form has some checkbox inputs. I thought I could perhaps check if a certain value exists in data.array, then add 'checked' to the box if it does. However, I have no idea if something like this is possible (without using mixins at least, as I can't get them to work in-line), or if there is an easier way to populate the checkboxes.
Checkbox item:
input.form-control(type="checkbox" value="item1" name="items")
Mixin:
mixin inArray(array, value)
- for (var i = 0; i < array.length; i++)
if array[i] === value
| checked
Attempted:
input.form-control(type="checkbox" value="item1" name="items" #[+inArray(object.items, 'item1')])
HTML result:
<input type="checkbox" value="item1" name="items" #[+inarray(object.items="" item1')]="" class="form-control">
Upvotes: 2
Views: 670
Reputation: 8361
You could use Array.includes() method in conjunction with boolean attribute to check only checkboxes that contain a specific value:
input.form-control(type="checkbox" value="item1" checked=object.items.includes('item1'))
Upvotes: 1