0

I have the following code

$('#bnp-agree').keyup(function(e){
  if(!$(this).is(':checked').val())
    e.preventDefault();
}).focusout(function(){
  if(!$(this).is(':checked').val()){
    $('#bnp-agree-wrong').slideUp();
  } else {
    $('#bnp-agree-wrong').slideDown();
  }
});

i have the following div

<div class="text-danger" id="bnp-agree-wrong" style="display: none;">Трябва да се съгласите с общите условия на БНП Париба!</div>

I want just to validate that the user is checked this ckeckbox

<label><input type="checkbox" name="bnp-agree"><span>Прочетох и се съгласих</span></label>

How can i achieve it?

  • `is(':checked')` returns true/false. You don't need `val()` there. Also that could be simplified to just `this.checked` which is also a boolean true/false – Taplar May 14 '18 at 21:16
  • https://stackoverflow.com/questions/901712/how-to-check-whether-a-checkbox-is-checked-in-jquery Isn't that exactly what you're looking for? – Félix Paradis May 14 '18 at 21:23

1 Answers1

1

I think your problem is simple :) THERE IS NO id="bnp-agree" ATTRIBUTE IN THE CHECKBOX

Also thats an alternative;

$('#bnp-agree').on("change",
function(){
  if($(this).prop("checked")){
    $('#bnp-agree-wrong').slideUp();
  } else {
    $('#bnp-agree-wrong').slideDown();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div class="text-danger" id="bnp-agree-wrong" style="display: none;">Трябва да се съгласите с общите условия на БНП Париба!</div>

<label><input type="checkbox" name="bnp-agree" id="bnp-agree"><span>Прочетох и се съгласих</span></label>
Serhat MERCAN
  • 1,078
  • 3
  • 14
  • 31