Is it possible to place the contents of a "Thymeleaf object" inside a "variable", in HTML?

Asked

Viewed 1,224 times

1

To put it in context:

I have a "log" object, which was declared on an HTML page through Thymeleaf:

<html xmlns:th="http://www.thymeleaf.org" th:include="layout :: page">
  ...
 <form class="form-inline" action="#" th:action="@{'log/'}" th:object="${log}" method="post">

In a certain part of the code is displayed the contents of this object, which is nothing more than a text "stringão":

<p th:utext="${log.content}">Log content</p>

Contents:

1. a
2. b
3. c
...

My doubt:

It is possible to place this content within a variable, and then use it within a . js?

Something like:

HTML:

<input type="hidden" th:value="${log.content}" >

Js:

var aux = $('input');
document.getElementById('content').innerHTML = aux.value;

My goal is to be able to handle this "log.content" outside of HTML, it is possible?

  • You want to take the value of the Hidden input is this?

  • That, I want to put ${log.content} inside some variable and use it. I used <input> only as an example, but it would also do!

1 answer

0


As inside this input will have a value brought by th:value="${log.content}". You can take the value in several ways, by id, for class, usually with Javascript or jQuery:

var valorInput1 = $('input:hidden').val(); // jQuery
console.log(valorInput1);                 // pega o valor do input que está com display none

var valorInput2 = document.getElementById('valorDoInput').value; // Javascript
console.log(valorInput2); // pega o valor do input com um id
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="hidden" th:value="${log.content}" value="10">

<input type="hidden" id="valorDoInput" th:value="${log.content}" value="20">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.