John John - 4 years ago 132
Javascript Question

check if a checkbox inside a label is ticked or not

I have a checkbox inside a label

<label class="selectit"><input value="84" name="download_category[]" id="in-download_category-84" type="checkbox"> CPU, HDD, Ram</label>


I want to check if the checkbox is check or not without using checkbox id.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script>
$(document).ready(function(){
$(".selectit").click(function(){
//Go and check if checkbox is check.
//If checkbox is tick
alert(this.Text());
});
});
</script>


How can I do it?

Answer Source

Please try this

$(".selectit").click(function() {
  if ($(this).find('input[type=checkbox]').is(":checked")) {
    alert($(".selectit").text());
  }
});

DEMO

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download