Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7027119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:12:22+00:00 2026-05-28T00:12:22+00:00

I have two checkboxes and one of the checkboxes must be checked. I can

  • 0

I have two checkboxes and one of the checkboxes must be checked. I can see that it’s right, no syntax errors. What should be made to my code to check if none of the checkboxes were checked?

HTML :

<input type="checkbox" value="aa" class="first" name="a"> Yes<br/>
<input type="checkbox" value="bb" class="second" name="b"> No <br/>
<button type="submit">Go!</button>
<p class="error"></p>

JavaScript:

$('button').on('click',function(){
    if( $(".first:not(:checked)") && $(".second:not(:checked)") ){
        $('.error').text('You must select atleast one!');
    }else
        $('.error').hide();
});

Example : http://jsfiddle.net/ptbTq/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:12:22+00:00Added an answer on May 28, 2026 at 12:12 am

    You are using selectors which do not return boolean values which is what you need when writing an if condition. Here’s what you could do:

    $('button').on('click',function() {
        if(!$(".first").is(":checked") && !$(".second").is(":checked")) {
            $('.error').text('You must select atleast one!').show();
        } else {
            $('.error').hide();
        }
    });
    

    or if you prefer and think it could be more readable you could invert the condiciton:

    $('button').on('click',function() {
        if($(".first").is(":checked") || $(".second").is(":checked")) {
            $('.error').hide();
        } else {
            $('.error').text('You must select atleast one!').show();
        }
    });
    

    Also notice that you need to .show() the error message in the first case as you are hiding it in the second.

    And here’s a live demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a codeigniter form that contains two checkboxes: <input type=checkbox checked=checked id=box1 name=box1
I have a GridView gvpodetails. In that I have two checkboxes one in the
I have two checkboxes. each one have a sub form that is disable when
I have two checkboxes. I need to check that at least one of them
Here is my code, I have two checkboxes and want to keep one disabled
I have the following two code blocks. Code block 1 var checkboxes = $(div.c1
I have two tables, one containing a list of different options users can select
I have two checkboxes name check1 and check2. I wanted for either one to
I have two columns one for id & other for checkboxes. i have taken
If I have a form with two checkboxes that have have the same array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.