0

I am trying to insert HTML code with JavaScript, but the Browser does not show Thymeleaf rendered

$('<span [[$ th:text#{some.property.key}]]  id="counter" class="UI-modern"> 
   </span>').insertAfter("#article");

HTML code

<p id="article">Example text</p>

The key some.property.key is the property. In Thymeleaf it would look like this:

<span th:text="#{some.property.key}" id="counter" class="UI-modern">
</span>

I would like to write the above code with JavaScript jquery insertAfter() below the id "article"

2
  • Java =/= JavaScript, so please don't add unrevelant tag ;) Commented Jan 27, 2022 at 17:13
  • Oh sorry. For the tag mistake Commented Jan 28, 2022 at 8:40

2 Answers 2

1

Intead of trying to use Thymeleaf HTML attributes in JavaScript, you can use JavaScript inlining and build your HTML that way. For example:

<script th:inline="javascript">
  let text = /*[[#{some.property.key}]]*/ "";
  $('<span id="counter" class="UI-modern"></span>').text(text).insertAfter("#article");
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it works! let text = /*[[#{some.property.key}]]*/ ;
1

Thymeleaf does not work on .js files It is not clear why you can't just rebder this element using Thymeleaf like the example you gave. You can give that span an ID and then reference it with JS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.