Home > Software engineering >  Detecting the change in inputs (jQuery)
Detecting the change in inputs (jQuery)

Time:12-16

There are several inputs and I want it to detect the variability in the input and give it the result. Currently, it works statically, I want to make it dynamically detect the digit change in the inputs.

I can do this with only 1 input but how can I do it for multiple inputs?

var allHaveSameValue = $(".inputclass").toArray().every(function(input, index, inputs) {
  return input.value === inputs[0].value;
});

console.log(allHaveSameValue);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text"  value="1"/>
<input type="text"  value="1"/>
<input type="text"  value="1"/>
<input type="text"  value="1"/>

CodePudding user response:

You can listen to the input event to run the check.

let $inputClass = $('.inputclass');
$inputClass.on('input', function(e){
  let val = $(this).val();
  let allHaveSameValue = $inputClass.toArray().every(input => input.value === val);
  console.log(allHaveSameValue);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text"  value="1"/>
<input type="text"  value="1"/>
<input type="text"  value="1"/>
<input type="text"  value="1"/>

  • Related