Skip to content
Advertisement

Is it possible to use thymeleaf variables to sum them in JS?

Here is the form I have ( many dishes where each has an input number ( quantity of dishes to order ).

The problem is that I would like to show the price of an order before the order is done, for this I need to sum somehow input of the ${dish.price}, but how can this be done?

JavaScript

JS ( Just inputs +1 to input type"number" when the button is clicked )

JavaScript

Advertisement

Answer

Of course yes, it is possible.

To get server data inside your thymeleaf templates, you can proceed various ways, and it depends on the context where you need that data.

Inside Javascript, (CSP “script-src ‘unsafe-inline’ …” or <script th:inline="javascript" nonce="" ...> are required because writing JS inline is a bad way regarding all XSS caveats. Thanks OWASP), you can try to write : let productCategory = /*[[${productCategory}]]*/ '';

Inside Javascript with ES6+ modules (with webpack or any other solution), you can check an endpoint of your webapp (using Promise feature with polyfill) which will load data inside a form input element. And make your operations as usual. Have fun.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement