1

This is HTML:

<select class="form-control selectVitrat" name="PVSuperior" id="PVSuperior">
      <option value="NO">NU</option>
      <option value="YES">DA</option>
    </select>
<select class="form-control selectVitrat" name="PVIntermediar9" id="PVIntermediar9">
      <option value="NO">NU</option>
      <option value="YES">DA</option>
    </select>
<select class="form-control selectVitrat" name="PVIntermediar8" id="PVIntermediar8">
      <option value="NO">NU</option>
      <option value="YES">DA</option>
    </select>

How can I count how many Yes I selected from these multiple selections?

3
  • I see no JavaScript here. What have you tried? Commented Aug 1, 2017 at 13:21
  • var count = $(".selectVitrat :selected").length; Commented Aug 1, 2017 at 13:22
  • It returns all my selections number Commented Aug 1, 2017 at 13:22

1 Answer 1

2

In JavaScript, you can use getElementsByTagName to get all the select. And using for loop, you can check how many select have YES values:

function checkTotalYes() {
  var selectElements = document.getElementsByTagName("select");
  var count = 0;
  for (var i = 0; i < selectElements.length; i++) {
    if (selectElements[i].value == 'YES')
      count++;
  }
  console.log('Total Yes: ' + count);
}

checkTotalYes();
<select class="form-control selectVitrat" name="PVSuperior" id="PVSuperior" onchange="checkTotalYes()">
  <option value="NO">NU</option>
  <option value="YES">DA</option>
</select>
<select class="form-control selectVitrat" name="PVIntermediar9" id="PVIntermediar9" onchange="checkTotalYes()">
  <option value="NO">NU</option>
  <option value="YES">DA</option>
</select>
<select class="form-control selectVitrat" name="PVIntermediar8" id="PVIntermediar8" onchange="checkTotalYes()">
  <option value="NO">NU</option>
  <option value="YES">DA</option>
</select>

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

Comments

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.