1

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">

3
  • 1
    You need to set the value property of the field, not the quantity_spots_n variable. 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 Commented Jul 29, 2020 at 10:05
  • @RoryMcCrossan thanks rory I tried placing it right before </body> but doesn't work. Commented Jul 29, 2020 at 10:07
  • @RoryMcCrossan sorry it does call it now but it is not changing the value. Commented Jul 29, 2020 at 10:09

3 Answers 3

1

Firstly use unobtrusive event handlers instead of onX attributes. The latter are outdated and no longer good practice. You can also use the input event to have the UI be more responsive to user input. In addition you can use the unary operator as a shorthand for the Number() function, and || to default the value to 1.

To solve your issue you need to set the value property of the second field directly as it holds a string value, not a reference to the value property. Also, use Math.max() to set the second field to the higher of the two values.

var quantity_gates = document.getElementById('quantity_gates');
var quantity_spots = document.getElementById('quantity_spots');

quantity_gates.addEventListener('input', function() {
  quantity_spots.min = this.value;
  quantity_spots.value = Math.max(this.value, +quantity_spots.value || 1);
});

quantity_spots.addEventListener('change', function() {
  this.value = Math.max(+this.value, +this.min);
});
<input type="number" 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">

Sign up to request clarification or add additional context in comments.

2 Comments

AMAZING, Thanks! How can I actually block user from decreasing the second number afterwards?
Set the min on the field to match the other's value. You would also need a change event handler to set the value equal to the min if the user manually typed a lower value. I've updated the example in the answer.
1

You need to asign the min in the second field : by adding in the end of your function

quantity_spots.value=quantity_spots_n; // to assign value
quantity_spots.min=quantity_spots_n; // to prevent user from decrasing the value

Comments

1

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));
  quantity_spots.min= quantity_spots_n //Sets the new minimum
  quantity_spots.value= quantity_spots_n //Sets the value 
}
<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">

1 Comment

thanks. I can still input a lower number but it works with the arrows so I cant decrease it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.