I’m creating a checkbox for every item in an XML file. The text of each file is "TRUE" or "FALSE", and I want to create a checked checkbox for every "TRUE" and an unchecked checkbox for every "FALSE". Right now my code is duplicated inside an if and an else because I can’t think of a way to conditionally include the checked parameter in that JSON. Does anybody know a more DRY way to accomplish the same thing?
d = $(this)
if (d.text() === 'TRUE') {
$('<input>', {
className: 'checkbox',
type: 'checkbox',
id: d.attr('id'),
name: d.attr('name'),
checked: 'checked'
}).appendTo(td);
}
else {
$('<input>', {
className: 'checkbox',
type: 'checkbox',
id: d.attr('id'),
name: d.attr('name')
}).appendTo(td);
}
You could also do it like this:
There’s no “rule” that states attribute maps can’t be built ahead of time. 🙂