Interactivity
This commit is contained in:
parent
4ffe7b8673
commit
959364fa12
26
README.md
26
README.md
@ -66,3 +66,29 @@ Thymeleaf parses the `greeting.html` template and evaluates the `th:text` exp
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
# Interactivity
|
||||
|
||||
Let's add minimal interactivity to the page by introducing an input field and a button. On the button press, a simple JavaScript will reload the page (by calling [[README#Web Controller]] 's `Get` endpoint) providing a value from the input filed as a URL parameter.
|
||||
|
||||
```html
|
||||
...
|
||||
<body>
|
||||
<p th:text="'Hello, ' + ${name} + '!'" />
|
||||
<input value="user"/>
|
||||
<button>Get your greeting</button>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
<script>
|
||||
function getName() {
|
||||
const name = document.querySelector('input').value;
|
||||
console.log(name);
|
||||
|
||||
window.open('http://localhost:8080/greeting?name='+name)
|
||||
}
|
||||
|
||||
const button = document.querySelector('button');
|
||||
button.addEventListener("click", getName);
|
||||
</script>
|
||||
```
|
@ -6,5 +6,19 @@
|
||||
</head>
|
||||
<body>
|
||||
<p th:text="'Hello, ' + ${name} + '!'" />
|
||||
<input value="user"/>
|
||||
<button>Get your greeting</button>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
<script>
|
||||
function getName() {
|
||||
const name = document.querySelector('input').value;
|
||||
console.log(name);
|
||||
|
||||
window.open('http://localhost:8080/greeting?name='+name)
|
||||
}
|
||||
|
||||
const button = document.querySelector('button');
|
||||
button.addEventListener("click", getName);
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user