Solution 1 :

working code:

<!DOCTYPE html>
<html>
<body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <div class="selected">
    <select class="form-control" id="select_one" onchange="enableButton()">
      <option value="received">received</option>
      <option value="complete">complete</option>
    </select>
    <select class="form-control" id="select_two" onchange="enableButton()">
      <option value="received">received</option>
      <option value="complete">complete</option>
    </select>
  </div>
</form>

<button id="btn_completed" disabled>Completed</button>

</body>
<script>
function enableButton() {
debugger;
  var all_statuses = document.body.querySelectorAll(".selected > .form-control");
  var option_two = "complete";

  var isdisabled = false;
  for (var i = 0; i < all_statuses.length; i++) {
    console.log(i + " This will work")
    if (all_statuses[i].value != option_two) {
      isdisabled = true;
      break;
    } 
  }

  if(isdisabled) {
    document.getElementById("btn_completed").disabled = true;
  }
  else {
document.getElementById("btn_completed").disabled = false;
    } 

}

$(document).ready(enableButton);
</script>
</html>

Problem :

This snippet displays two select elements, and a submit button. Initially, button is disabled.

Desired behavior: enable submit button when both elements have second option (complete) selected. If either one of the elements has first option (received) selected, disable the button.

Current behavior: button is enabled/disabled regardless of the first select element. Meaning: If I select option received in the first dropdown, and option complete in the second, button will be enabled, instead of disabled.

function enableButton() {
  var all_statuses = document.body.querySelectorAll(".selected > .form-control");
  var option_two = "complete";
  for (var i = 0; i < all_statuses.length; i++) {
    console.log(i + " This will work")
    if (all_statuses[i].value == option_two) {
      document.getElementById("btn_completed").disabled = false;
    } else document.getElementById("btn_completed").disabled = true;
  }
}

$(document).ready(enableButton);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <div class="selected">
    <select class="form-control" id="select_one" onchange="enableButton()">
      <option value="received">received</option>
      <option value="complete">complete</option>
    </select>
    <select class="form-control" id="select_two" onchange="enableButton()">
      <option value="received">received</option>
      <option value="complete">complete</option>
    </select>
  </div>
</form>

<button id="btn_completed">Completed</button>

So, the question is: How to enable the button if all select elements have option complete selected, or how to disable the button if at least one select element has option different than complete selected?

Comments

Comment posted by mplungjan

Also NEVER call anything in a form

Comment posted by Antti

Not really sure what you are trying to do, but are you setting same button enabled and disabled multiple times? Maybe you should use break; once disabled?

Comment posted by CBroe

The select field you have shown contains two options with the values

Comment posted by mplungjan

1. getting console errors in the snippet I made (definition of btnComplete is missing) 2. There is no onload attribute on a form 3. Never call anything submit 4. Whatever you called submit is missing from your code. 5 Why ID on the options? 6. Why not a normal ID, # needs to be escaped when trying to use it in a selector

Comment posted by Antti

Set initial to enabled and if some of those values are not correct then disable it and use break; otherwise it continues the loop and the last item will decide is it enabled or disabled.

By