Bogdan Gerashchencko
Bogdan Gerashchencko

Reputation: 23

Checkbox attribute checked in Thymeleaf

How I can set a checked attribute for an input in my form, using Thymeleaf? Here is my code, which currently doesn't work:

<label th:each="cat : ${categories}">
    <input type="checkbox" value="" 
        th:value="${cat.id}" 
        th:text="${cat.description}"
        th:checked="${recipe.getCategories().contains(cat) ? true : false}"
    />
</label>

Upvotes: 1

Views: 3149

Answers (1)

Mahozad
Mahozad

Reputation: 24482

As stated in comments, the problem may be from somewhere else but try this and see if it helps:

th:checked="${recipe.getCategories().contains(cat)}"

Upvotes: 1

Related Questions