0

In my messages I have:

resetPassword.sending.email=Sending email
resetPassword.sending.sms=Sending sms

I want to use it in the HTML and JQuery but I am unable to pass the variable to form the message. I will have multiple messaging methods so I can't just use if else statement, it has to be dynamic.

<script th:inline="javascript">

$("#sendButton").click(function(e) {
    sedingMethod = $(this).data('method'); // can be 'sms' or 'email'
    $("#sendingMessage").text([[#{resetPassword.sending.[sedingMethod]}]]);
});
</script>

2 Answers 2

1

You cannot. Thyemelaf is processed at the server and jQuery runs on client. One possible way is to get all the relevant messages into the Javascript and then use it in jQuery. Something like:

<script th:inline="javascript">
 var resetPasswordSendingEmail = /*[[#{resetPassword.sending.email}]]*/'';
 var resetPasswordSendingSms = /*[[#{resetPassword.sending.sms}]]*/'';
 var messageMap = { 'email' : resetPasswordSendingEmail, 'sms': resetPasswordSendingSms };
</script>

Then in the JS you can pick your variable without the need of if..else as shown below:

var message = messageMap[sedingMethod];
Sign up to request clarification or add additional context in comments.

Comments

0

See here:

https://www.concretepage.com/thymeleaf/thymeleaf-javascript-inline-example-with-variable

So I believe this would work:

$("#sedingMessage").text([[${resetPassword.sending.sendingMethod}]]);

1 Comment

I tried this (replacing $ with # for the messages), but sendingMethod is not passed for it's value, but it's looking for message with the key resetPassword.sending.sendingMethod

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.