0

In my page have a modal window, in the modal i have 2 checkboxes, i want if all checkboxes selected enabled send button and change background color (if disabled bgcolor is gray else bgcolor red). How i can it right way ?

HTML:

<form action="" method="POST" class="send-modal-data">
    <input type="text" id="send_email" name="subscribe-email" class="modal-input" placeholder="Email *" required="required">
    <div class="checkboks custom-sq vdc-cb-area">
        <input type="checkbox" id="box4" name="vdc-modal-cb" class="checked-checkbox"/>
        <label for="box4" class="checkboks-text d-flex align-center"><?php echo the_field('vdc_checkbox_txt', 'option'); ?></label>
    </div>
    <div class="checkboks custom-sq vdc-cb-area">
        <input type="checkbox" id="box7" name="vdc-modal-cb" class="checked-checkbox" />
        <label for="box7" class="checkboks-text d-flex align-center"><?php echo the_field('vdc_checkbox_text_2', 'option'); ?></label>
    </div>
    <div class="success-msg">
        <div class="msg"></div>
    </div>
    <input type="submit" name="subscribe-form" id="vdc-send-modal" class="danger-btn send-subscribe" disabled="disabled" value="<?php echo the_field('lets_get_started', 'option'); ?>"></input>
</form>

JS:

var checks = document.getElementsByName('vdc-modal-cb');
var checkBoxList = document.getElementById('vdc-cb-area');
var sendbtn = document.getElementById('vdc-send-modal');

function allTrue(nodeList) {
  for (var i = 0; i < nodeList.length; i++) {
    if (nodeList[i].checked === false) return false;
  }
  return true;
}

checkBoxList.addEventListener('click', function(event) {
  sendbtn.disabled = true;
  if (allTrue(checks)) sendbtn.disabled = false;
  console.log(123);
});

NOTE: I took this example from the stack overflow but it doesn't work for me

1
  • Have you checked your DevTools? Commented Dec 2, 2019 at 11:16

1 Answer 1

1

1.You should use getElementsByClassName to get elements with the same class.

2.To add eventListener to the class elements, you should iterate over the elements.

var checks = document.getElementsByName('vdc-modal-cb');
var checkBoxList = document.getElementsByClassName('vdc-cb-area');
var sendbtn = document.getElementById('vdc-send-modal');

function allTrue(nodeList) {
  for (var i = 0; i < nodeList.length; i++) {
    if (nodeList[i].checked === false) return false;
  }
  return true;
}

for (var i = 0; i < checkBoxList.length; i++) {
  checkBoxList[i].addEventListener('click', function(event) {
    sendbtn.disabled = true;
    if (allTrue(checks)) sendbtn.disabled = false;
  });
}
Sign up to request clarification or add additional context in comments.

2 Comments

@shreyes Thanks for quickly feedback, thanks for code it's work, but how i can change bg color when disabled bgcolor is gray ?

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.