0

I've got a box with checkboxes and I wanted that button 'submit' appear only if one checkbox was marked, When more than one box is checked the button 'submit' must disappear, I've tried code it using addClass and removeClass provides by Jquery, here's my code for now:

$('.a').click(function() {
  $('.c').removeClass('b');
});
.b {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="checkbox">
  <form method="post">
    <fieldset>
      <legend>What is Your Favorite Pet?</legend>
      <input type="checkbox" name="animal" value="Cat" class="a" />Cats
      <br />
      <input type="checkbox" name="animal" value="Dog" class="a" />Dogs
      <br />
      <input type="checkbox" name="animal" value="Bird" class="a" />Birds
      <br />
      <input type="submit" value="Submit now" class="c b" />
      <button>Cancel</button>
    </fieldset>
  </form>
</div>

6
  • 6
    If I may be so blunt, since you clearly only want 1 of the fields to be submitted, why are you using checkboxes? Checkboxes are typically used when the user is allowed to check more than one field. If you only want to submit when the user selected one, why not restrict the user to only being able to select one using radiobuttons? Commented Jan 8, 2016 at 12:49
  • @Glubus, Because my use case got this flux, When I check one checkbox the button submit have to appear, I've got to follow my use case with no exceptions, thanks for another solution. Commented Jan 8, 2016 at 13:03
  • I see, sucks that you can't suggest changes to the use case. This result will most likely inflict confusion on the users this feature will be used by. Commented Jan 8, 2016 at 13:06
  • Yep, but It's cool discuss about it, I'm new in front end and I wanna know more about this area. Commented Jan 8, 2016 at 13:12
  • 1
    Front end becomes increasingly more important. If you're getting into the market of online marketing, you might find the area of conversion interesting. It's the study of getting people that visit your website to actually buy stuff from you instead of just browsing. It has everything to do with your situation Commented Jan 8, 2016 at 13:15

2 Answers 2

3

You are doing it wrong. If you want only one check box to be selected, you need to attach the event on .a, that's right. But... See the below code:

$('.a').click(function() {
  if ($(".a:checked").length == 1)
    $('.c').removeClass('b');
  else
    $('.c').addClass('b');
});
.b {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="checkbox">
  <form method="post">
    <fieldset>
      <legend>What is Your Favorite Pet?</legend>
      <input type="checkbox" name="animal" value="Cat" class="a" />Cats
      <br />
      <input type="checkbox" name="animal" value="Dog" class="a" />Dogs
      <br />
      <input type="checkbox" name="animal" value="Bird" class="a" />Birds
      <br />
      <input type="submit" value="Submit now" class="c b" />
      <button>Cancel</button>
    </fieldset>
  </form>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

You want to toggle the submit button based on the condition $('.a:checked').length != 1

$('.a').on('change', function () {
  $('.c').toggleClass('b', $('.a:checked').length != 1);
});
.b {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="checkbox">
  <form method="post">
    <fieldset>
      <legend>What is Your Favorite Pet?</legend>
      <input type="checkbox" name="animal" value="Cat" class="a" />Cats
      <br />
      <input type="checkbox" name="animal" value="Dog" class="a" />Dogs
      <br />
      <input type="checkbox" name="animal" value="Bird" class="a" />Birds
      <br />
      <input type="submit" value="Submit now" class="c b" />
      <button>Cancel</button>
    </fieldset>
  </form>
</div>

Comments

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.