1

How to check whether checkbox in particular DIV is checked or not?

<div id="TeddyTab">
 <input type="checkbox" id="bla" />
 <input type="checkbox" id="bla1" />
 <input type="checkbox" id="bla2" />
</div>

JQuery?

 $(':checked').each(function() {
        if(this.checked === 'true')
            $(this).closest('div').next('div').find('textarea').prop('readonly',false);
    });

The above jquery checkes everything n page which i do not want. I want to check in above div only?

fatherazrael
  • 5,511
  • 16
  • 71
  • 155

5 Answers5

1

use the below code to validate in particular div

 if($("#TeddyTab input:checked").size() > 0){
// do here what ever you wanna do
}
dom
  • 1,086
  • 11
  • 24
0

you are comparing boolean with string value as this.checked return boolean value. also you are only iterating over checked element which eliminates the need of using if condition

$(':checked').each(function() {
    $(this).closest('div').next('div').find('textarea').prop('readonly',false);
});
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Without much changes; Here is how it worked:

$('#TeddyTab :checkbox').each(function() {
 if(this.checked)
 $(this).closest('div').next('div').find('textarea').prop('readonly',false);
});
fatherazrael
  • 5,511
  • 16
  • 71
  • 155
0

Posible duplicated jQuery if checkbox is checked? Anyway, this is how you should implement it:

$('#TeddyTab input[type="checkbox"]:checked').each(function(i,input){
      $(input).closest('div').next('div').find('textarea').prop('readonly',false);

or if you need the IF statement:

$('#TeddyTab input[type="checkbox"]').each(function(i,input){
            if($(input).is(':checked')){ 
                $(input).closest('div').next('div').find('textarea').prop('readonly',false);
Community
  • 1
  • 1
Juan Girini
  • 1,150
  • 2
  • 16
  • 31
0

Try This :

DEMO : http://jsfiddle.net/K6Wvk/

$(document).ready(function () {

$('#formid').validate({ // initialize the plugin
    rules: {
        'test[]': {
            required: true,
            maxlength: 2
        }
    },
    messages: {
        'test[]': {
            required: "You must check at least 1 box",
            maxlength: "Check no more than {0} boxes"
        }
    }
});

});