Search code examples
meteormeteor-blaze

Iterating a checkboxes using {{#each)) in blaze (Meteor)


I have the following code`

{{# each item}}
    <p class="center">
     <input type="checkbox"  checked="checked" id="basiccbox" name={{id}} />
     <label for="basiccbox"> &nbsp;</label>
    </p>
{{/each}}

Multiple check boxes is now rendered. However, when i click any of the box, only the first check box toggles between the true or false states. This happens because of the

for="basiccbox"

and

id="basiccbox"

i.e all box that is rendered have the same Id. How do i generate unique ids or how does one deal with such a situation.

`


Solution

  • usually you should assign the item._id to id of checkbox tag. below is the code to solve your issue.

    {{# each item}}
        <p class="center">
           <input type="checkbox"  checked="checked" id={{_id}} name={{id}} />
           <label for="basiccbox"> &nbsp;</label>
        </p>
    {{/each}}
    

    It is good to bind the document id somewhere for tag element, because you can use the same id to do DB operations as well.