I am trying to block a button if an input is empty when a certain option is selected from a select
Let me explain: I have a select with 3 options which are pending, approved, rejected. when approved is selected an input appears in which I must enter a number value but if that input is empty the button must be blocked but if you select any other option from the select it is enabled
Code
<div>
<label for="status">Estado del proceso</label>
<select name="status" id="status" onChange="statuses()">
<option value="pendiente">Pendiente</option>
<option value="aprobado">Aprobado</option>
<option value="rechazado">Rechazado</option>
</select>
</div>
<div id="honorary" style="display:none;">
<input type="number" name="honorary" id="fee">
</div>
</div>
<input type="submit" value="Editar" id="update" class="btn btn-success" >
<script type="text/javascript">
function statuses() {
var x = document.getElementById("status").value;
if ( x == 'aprobado') {
document.getElementById("honorary").style.display = "";
}else{
document.getElementById("honorary").style.display = "none";
document.getElementById("update").disabled = false;
}
}
</script>
If what you need is that, once the approved button is selected and the input with id 'fee' has no value, it is disabled, you would need the following:
For one thing, in your HTML code , you would need to add the property
onchange
to the input#fee
, so that you could catch the event when its value changes:On the other hand, in your Javascript code , you would need a function that evaluates if the input
#fee
contains any value and enables or disables the button#update
:Part 0
change
value
that each labeloption
contains and that will serve us to do the filtering and subsequent logicPart 1
select
corresponds to approved then:input
with its propertydisabled
div
one that contains the buttonCode
Initially your button should be disabled:
And then you can do this:
You can disable a button using the disabled attribute, so in a few words what is done there is to extract the elements that interest us, add a listener event to our input, the listener event is of type input (it is executed every time the value of the input changes), then, in the disableIfNotAble function, what is done is to check if the input has content, if it does, we set the disabled to false, but if not, we block the button, setting the disabled to true.