From the course: HTMX with Thymeleaf in Spring Boot

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Set HX-Retarget using HttpServletResponse

Set HX-Retarget using HttpServletResponse - Spring Boot Tutorial

From the course: HTMX with Thymeleaf in Spring Boot

Set HX-Retarget using HttpServletResponse

- [Instructor] 043 begin records management open in IntelliJ. Let's talk about using HX Retarget for dynamic control of responses. Another helpful and interesting feature of HTMX response headers is the ability to change the HX target dynamically during response. If we look at the current editable email form, .HTML located under resources, templates, person, modify and consider the button tag that does the update. We can see that the HX target is set to email parent. This is good since we want to control how the form response will completely replace the current email parent. But let's say that we run into an issue where we want to validate that an email already exists for that person. Now, there could be various business reasons for this, but to illustrate how to keep the form in place and retargeting a different DOM ID with a warning or validation message from the controller, this could be very useful. Let's first make this a bit easier by creating a new fragment under fragments…

Contents