Select only ONE checkbox with vanilla javascript and bootstrap 4

Asked

Viewed 206 times

1

I have the following code that generates the printable form in the image...

<div class="form-check form-group" id="areaTermosCondicoes">
     <input type="checkbox" id="accept-term" class="form-check-input">
     <label for="accept-term" class="form-check-label">Li e concordo com os <a href="#">Termos de uso</a></label>
     <div class="invalid-feedback">
          <span class="alert-message"></span>
     </div>
     <div class="valid-feedback">
          <span class="alert-message"></span>
     </div>
     <button type="button" class="btn btn-secundary ml-2" id="cadastrar" onclick="cadastrar()" type="submit">Cadastrar-se como <span></span></button>
</div>

Formulário printado

I needed to get the data marking the terms and conditions of this form to validate also... But I’m not getting...

the code I’m using in Js is:

let termosCondicoes = document.querySelector('form#formCadastro #accept-term')
  • Just one question: why would it have to be in Vanilla since you’re using jQuery?

  • Why tb there are two Ivs <div class="valid-feedback">?

1 answer

1


Access the property .checked:

function checar(){
  let termosCondicoes = document.getElementById('accept-term');
  if(termosCondicoes.checked)
    alert('Li e concordo com os Termos de uso está marcado');
   else
   alert(' Li e concordo com os Termos de uso não está marcado');
}
<div class="form-check form-group" id="areaTermosCondicoes">
     <input type="checkbox" id="accept-term" class="form-check-input">
     <label for="accept-term" class="form-check-label">Li e concordo com os <a href="#">Termos de uso</a></label>
     <div class="invalid-feedback">
          <span class="alert-message"></span>
     </div>
     <div class="valid-feedback">
          <span class="alert-message"></span>
     </div>
     <button type="button" class="btn btn-secundary ml-2" id="cadastrar" onclick="checar()" type="submit">Checar <span></span></button>
</div>

  • show... coming home I test... even vlw!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.