-1

I have this below code where i need to check if first two checkbox are checked or not.

I need to check against their id's.

Like how to check if i the first two checkbox are checked or not.

<input type="checkbox" name="check1" id="check1id" tabindex="1" value="Test" />

<input type="checkbox" name="check2" id="check2id" tabindex="2" value="Test" />

<input type="checkbox" name="check3" id="check3id" tabindex="3" value="Test" />



var boxes = $('.checkbox input');
                if(boxes.length > 0) {
                    if( $(':checkbox:checked').length < 1) {
                        if($("#error-select").length == 0){
                            $("#error-checkbox").show();

                        }
                        return false;
                    }
                    else{
                        $("#error-checkbox").hide();
                    }

How to do this.

Regards

user2542953
  • 127
  • 1
  • 9
  • 1
    Possible duplicate of [Check if checkbox is checked with jQuery](http://stackoverflow.com/questions/2204250/check-if-checkbox-is-checked-with-jquery) – nicael Jul 28 '16 at 13:27
  • hey nicael can u alter the above jquery that i have written to match the requirement if you don't mind.. – user2542953 Jul 28 '16 at 13:29

2 Answers2

0

To find if a check box is checked you can do with jquery is() selector

if($("#check1id").is(':checked') || $("#check2id").is(':checked'))
{
alert("You have checked the checkbox check1id")
}
bhanu.cs
  • 1,345
  • 1
  • 11
  • 25
0

You can use jQuery prop() to find whether checkbox is checked or not.

$('input[type="checkbox"]').on('click', function() {

  if ($('#check1id').prop('checked') == true && $('#check2id').prop('checked') == true) {
    
    console.log('check1id && check2id are checked');
    
  } else {
    
    console.log('both check1id & check2id are not checked');
    
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="check1" id="check1id" tabindex="1" value="Test" />

<input type="checkbox" name="check2" id="check2id" tabindex="2" value="Test" />

<input type="checkbox" name="check3" id="check3id" tabindex="3" value="Test" />
Arun CM
  • 3,345
  • 2
  • 29
  • 35
  • Hi Arun.. If i want to display error message in page like in div tag how to do thta.. – user2542953 Jul 28 '16 at 13:59
  • @user2542953 If my answer solved your question you can mark my answer by clicking the tick box near to my answer it will turn green after that. You can use bootstrap.and display error messages like this. `
    Success! Indicates a successful or positive action.
    `
    – Arun CM Jul 28 '16 at 14:13