I have two input fields and I want to reflect the value of the first input as a minimum value of the second one but it is not calling the function which I built for that reason:
var quantity_gates = document.getElementById('quantity_gates');
var quantity_spots = document.getElementById('quantity_spots');
function count_spots() {
var quantity_spots_n = quantity_spots.value;
var quantity_gates_n = quantity_gates.value;
quantity_gates_n = Number(quantity_gates_n);
quantity_spots_n = Number(quantity_spots_n);
quantity_spots_n = Math.min(quantity_gates_n, Math.max(1, quantity_gates_n));
}
<input type="number" onChange="count_spots()" id="quantity_gates" value="1" name="quantity_parking_gates" min="1" max="9999">
<input type="number" id="quantity_spots" value="1" name="quantity_parking_spots" min="1" max="9999">
valueproperty of the field, not thequantity_spots_nvariable. Also check that your JS code runs after the DOM has loaded. Put it before</body>or wrap it in a DOMContentLoaded event handler. Also note that 'ajax', 'jquery' and 'html' are not relevant to the problem so I removed the tags</body>but doesn't work.