Is it possible to disable fileds using th:disabled based on a value from the js file in thymeleaf

Viewed 34

I have few fields which need to be disabled based on a value which is being defined in the js file. Is it possible to do so?

2 Answers

Instead of setting disabled through javascript you could add the disabled to the HTML input element:

<input class="form-control" id="form_input" disabled="disabled" .... />

Then in your javascript:

document.getElementById('form_input').onchange = function () 
{
    if (this.value == '0') 
    {
        document.getElementById("form_input").disabled = true;
    }

    else 
    {
        document.getElementById("form_input").disabled = false;
    }
}

not sure if it just in JS possible, but with react:


function() {
const [value, setValue] = useState(false)

const handleChange= () => {
setValue(!value)
}

return(
<>
<input   type="checkbox" onClick={handleChange}/>
<input   type="text" disabled={value}/>
</>
)}

then you validate the value with a function in your script.

Related