1

I'd like to amend my script so if ANY of the 2 checkboxes are selected, it says 'Woo hoo!'. Where am I going wrong?

HTML:

0:<input type="checkbox" name="remove_0" id="remove_0">
<br>
1:<input type="checkbox" name="remove_1" id="remove_1">
<br><br>
<button id="submitTCP">
Hit me!
</button>

Javascript:

$("#submitTCP").click(function(event){

  event.preventDefault();

  if ($('input:checkbox', this).is(':checked')) {
    alert('Woo hoo!');
  } else {
      alert('Please select something!');
      return false;
  }

})

JSFiddle:

https://jsfiddle.net/6qh0c8fm/

2 Answers 2

3

your this is parent submit button not checkbox

$("#submitTCP").click(function(event){

    event.preventDefault();

    if ($('input:checkbox').is(':checked')) {
        alert('checkboxes checked');
    } else {
        alert('Please select something!');
        return false;
    }

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
0:<input type="checkbox" name="remove_0" id="remove_0">
<br>
1:<input type="checkbox" name="remove_1" id="remove_1">
<br><br>
<button id="submitTCP">
    Hit me!
</button>

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

1 Comment

D'oh. Thank you so much, Krishna :-)
1

There is no peramanent function to check either checkbox is checked but it can be done using click and getting it's checked property.

 $('#cb').click(function () {
        if (this.checked == true) {
            console.log('checked');
        }
        else {
            console.log('un-checked');
        }
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="cb"/>

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.