Issue
Fragment
<div th:fragment="sort (label,field)">
<label th:text="${label}"> Label
<input th:name="'sort-' + ${field}" type="radio" value="asc"/>
<input th:name="'sort-' + ${field}" type="radio" value="dsc"/>
</label>
</div>
Usage
<div th:replace="fragments/form.html :: sort(label = 'Name', field = 'name')"></div>
What appears on the page
<label>Name</label>
Solution
Don't place your <input>
elements inside the <label>
element.
When you use th:text="${label}"
in your <label>
element, the results of that Thymeleaf expression replace everything inside the <label>...</label>
tags - including the nested <input>
elements. That is why you only see <label>Name</label>
.
A label is more typically associated with an input element (or group of elements) using the for
attribute. You can read more about that in this question:
Using the HTML 'label' tag with radio buttons
Answered By - andrewJames
Answer Checked By - David Marino (JavaFixing Volunteer)