I am trying to create a form that will be enabled and disabled depending on the checkbox tick mark.
<div class="row">
<div class="col-md-12">
<p>Is this client user</p>
<input
type="checkbox"
name="is_user"
id="is_user"
onclick="enableCreateUser()"
/>
</div>
</div>
<div class="row" id="user_register">
<div class="form-group row">
<div class="col-md-6">
<label class="" for="username">Username:</label>
</div>
<div class="col-md-6">
<input class="form-control" type="text" name="username"/>
</div>
</div>
<div class="form-group row">
<div class="col-md-6">
<label class="" for="password">Password:</label>
</div>
<div class="col-md-6">
<input class="form-control" type="password" name="password"/>
</div>
</div>
</div>
I am trying to make this javascript or jquery function that will allow me to disable or enable this portion with id="user_register".
Here is the code that I tried.
function enableCreateUser() {
if (document.getElementById("is_user").checked) {
document.getElementById("user_register").disabled = true;
}
if (!document.getElementById("is_user").checked) {
document.getElementById("user_register").disabled = false;
}
}
Please help me complete this function. Thank you.
inputonly or the whole section ?divelement. You will need to loop over the input elements inside this and disable them each individually - or you need to group them using afieldset, that can be disabled. And don’t use onclick on checkbox elements, use onchange.