Using Spring MVC and Thymeleaf, I am constructing an html view with some javascript inside.
Within the page, th:each is used with iteration values to give a set of buttons a unique HTML id.
<td th:each="optionValue,iterStat : ${someObject.optionValues}">
<button class="btn btn-default" th:id="${'optionBtn_' + (iterStat.count - 1)}" th:text="${optionValue.toString()}" />
</td>
My problem comes when trying to generate javascript that will use a jQuery reference to each button id.
In 'another' view resolution language, I would use the code:
<% for(var i = 0; i < someObject.optionValues.length; i++) { %>
$('#optionBtn_<%- i %>').on("click", function() {
doSomething('<%= someObject.optionValues[i] %>');
});
<% } %>
(the above may not be 100% syntactically correct, but I hope you get the idea - what I'm trying to do is possible using the above style)
but in Thymeleaf, whilst I understand that I can use
th:inline="javascript"
to reference individual model items, I can't see how I can use a look to generate multiple jQuery function call definitions within a script block.
Any ideas? (I may be approaching the problem completely wrong, so am open to new ideas on that front too)