0

I have an Input which is of type number and want to check if its value has Decreased/Increased:

document.getElementById("myInput").addEventListener("change", function() {
    console.log(document.getElementById("myInput").value)

    // Check if Value has Increased/Decreased here
});
<input id="myInput" type = "number" value = "0">

Help would be Appreciated. Thanks!

5 Answers5

3

Store the value and compare it

var elem = document.getElementById("myInput");
elem.dataset.lastValue = elem.value;
elem.addEventListener("change", function() {

  const direction = +elem.value > +elem.dataset.lastValue ? 'increased' : 'decreased';
  console.log(direction);
  elem.dataset.lastValue = elem.value;
});
<input id="myInput" type="number" value="0">
epascarello
  • 204,599
  • 20
  • 195
  • 236
2

Add some logic to handle the difference

const inputNode = document.getElementById("myInput");
let initialValue = 0;
inputNode.addEventListener("change", function () {
    const currentValue = Number(inputNode.value);
    if (currentValue > initialValue) {
        console.log('Increased by ', currentValue - initialValue)
    } else if (currentValue < initialValue) {
        console.log('Decreased by ', initialValue - currentValue)
    } else {
        console.log('No Change')
    }
    initialValue = currentValue;
});
<input id="myInput" type="number" value="0">
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
2

var a = document.getElementById('myInput').value;
document.getElementById("myInput").addEventListener("change", function() {
  console.log(document.getElementById("myInput").value)

  var b = document.getElementById('myInput').value;
  if (a > b) {
    //value decreased do what you want to do when value decreased

  } else if (a < b) {
    // value increased do what you want when value increased
  } else {
    //value is equal do what you want here
  }
  a = b;
});
TheEagle
  • 5,808
  • 3
  • 11
  • 39
Lalan Kumar
  • 107
  • 6
2

Return a closure from your listener that manages the current value of your input. That way you don't have to deal with global variables.

const input = document.querySelector('#myInput');

// Call `handleChange` that returns a new function to
// be used as the listener function
input.addEventListener('change', handleChange(), false)

function handleChange() {

  // Set the initial value
  let value = 0;
    
  // This is the function that your listener will be calling
  // but, because it "saves" variables from its local
  // environment, you can update `value` without any issues
  return function () {

    // Coerce the string to a number
    const inputValue = Number(input.value);

    // Check to see if it's decreased or increased
    // based on the current value
    if (inputValue < value) {
      console.log('Decreased');
    } else {
      console.log('Increased');
    }

    // Set the value to the new number
    value = inputValue;

  }

};
<input id="myInput" type = "number" value = "0">
Andy
  • 61,948
  • 13
  • 68
  • 95
1

You could store the previous value in a variable, fetch the current one in your event handler, check if the new one is equal, smaller or greater than the stored one, and if new and stored one are not equal, update the stored one with the new value. Like this:

var oldValue = document.getElementById("myInput").value;

window.addEventListener("load", function() {
  document.getElementById("myInput").addEventListener("change", function() {
    var newValue = document.getElementById("myInput").value;
    if (oldValue == newValue) {
      console.log("Value was not changed, current:", newValue); /* this should normally never be the case */
    } else if (oldValue < newValue) {
      console.log("Value was increased from", oldValue, "to", newValue);
    } else if (oldValue > newValue) {
      console.log("Value was decreased from", oldValue, "to", newValue);
    }
    oldValue = newValue;
  });
});
<input id="myInput" type="number" value="0">
TheEagle
  • 5,808
  • 3
  • 11
  • 39