Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count number of inputs and sum depending on its values

I would like to do two things:

  1. I want to count the number of inputs that have a value. (doesn’t matter if the value is A or X).

  2. Then, count the number of inputs whose value is equal to A

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

therefore, the results should contain 6 of 14 items

This is what I tried to count the inputs that already have value:

var filledInputs = $(".col input").filter(function() {
  return !!this.value;
}).length;

const test2 = document.querySelectorAll(".result");
test2.forEach((item) => {
  item.innerHTML = filledInputs;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<div class="col">
  <input type="text" value="A">
  <input type="text" value="A">
  <input type="text" value="X">
  <input type="text" value="X">
  <input type="text" value="A">
</div>
<div class="col">
  <input type="text" value="A">
  <input type="text" value="X">
  <input type="text" value="A">
</div>
<div class="col">
  <input type="text" value="X">
  <input type="text" value="X">
  <input type="text" value="A">
</div>
<div class="col">
  <input type="text">
  <input type="text">
  <input type="text">
</div>

<div class="results"></div>

>Solution :

Use the same logic that you made to find the filled inputs, to find the inputs with value A

const filledInputs = $(".col input").filter(function () {
  return !!this.value;
}).length;
const inputWithValA = $(".col input").filter(function () {
  return this.value === 'A';
}).length;
console.log(filledInputs)
console.log(inputWithValA)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<div class="col">
  <input type="text" value="A" />
  <input type="text" value="A" />
  <input type="text" value="X" />
  <input type="text" value="X" />
  <input type="text" value="A" />
</div>
<div class="col">
  <input type="text" value="A" />
  <input type="text" value="X" />
  <input type="text" value="A" />
</div>
<div class="col">
  <input type="text" value="X" />
  <input type="text" value="X" />
  <input type="text" value="A" />
</div>
<div class="col">
  <input type="text" />
  <input type="text" />
  <input type="text" />
</div>

<div class="results">6 of 14 items</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading