Search code examples
htmlspring-bootthymeleaf

disable button based on checkbox state in thymeleaf and springboot


Based on checkbox state i want to enable button if this checkbox is checked and vice versa using thymeleaf with springboot. Here is what i tried :

        <div>
            <label>
            <input type="checkbox" th:value="${test}">
                actif
                </label>
        </div>
        <div>
            <button [disabled]="${test == false}"  type="submit">Envoyer</button>
        </div>
        

In my contoller :

        private boolean test = false;
        model.addAttribute("test", test);

Solution

  • As Wim Deblauwe has mentioned, the only way to do that properly is to use JavaScript. For this case it's relatively simple. See this answer for details