Display the input value of a factor of 10

I have this code:

<div class="number">
  <input type="text" onkeyup="separateNum(this.value,this);" value="{input number}">
  <span>box: {number}</span>
</div>

I want when the user enters a number in the input, it is divided by 10 and displayed in a span.
For example, if the user enters the number 21, the user will immediately see the number 2 (without any further decimal value)

12 thoughts on “Display the input value of a factor of 10”

  1. function onInputChange(e) {
      const span = document.getElementById('result');
      span.innerHTML = Math.floor(e.value / 10);
    }
    <div class="number">
      <input type="text" onkeyup="onInputChange(this)" value="22">
      <span id='result'>box: {number}</span>
    </div>
    Reply
  2. You can use Math.trunc to remove the decimal portion after dividing. This will also work correctly for negative numbers.

    document.querySelector('input').addEventListener('keyup', function(e){
      this.nextElementSibling.textContent = 'box: ' + Math.trunc(this.value / 10);
    });
    <div class="number">
      <input type="text">
      <span>box: </span>
    </div>
    Reply

Leave a Comment