1

How can I dynamically add checked property to my input checkbox, assuming my object has an active property?

{% for object in objects %}
    <li>
        <input type="checkbox" id="id_{{object.id}}" name="checkbox" value={{object.id}}>
    </li>
{% endfor %}

Adding checked="{{object.active}}" did not work.

2 Answers 2

2

To add the checked property to your checkbox input element, you can dynamically add it like this:

<input type="checkbox" id="id_{{object.id}}" name="checkbox" value={{object.id}} {{'checked' if object.active else ''}}>
Sign up to request clarification or add additional context in comments.

Comments

0

I was having a similar situation where a simple if-else worked smoothly.

{% for object in objects %}
<li>
    {% if object.active: %}
        <input type="checkbox" id="id_{{object.id}}" name="checkbox" value={{object.id}} checked>
    {% else %}
        <input type="checkbox" id="id_{{object.id}}" name="checkbox" value={{object.id}}>
    {% endif %}
</li>
{% endfor %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.