Sachin Dagar
Sachin Dagar

Reputation: 63

Disabling check box and uncheck it using jQuery

In below code I am dynamically generating td elements in for loop.

jQuery("#dialog_load_content").load(url, function() {
        var clientName = jQuery('#client option:selected').text();
        var clientId = Number(jQuery('#client option').filter(function() {return jQuery(this).html() == clientName;}).val());
        var navDate = jQuery('input:checkbox:checked.signOff').closest('tr').find('td:eq(2)').html();
        var fundName = jQuery('input:checkbox:checked.signOff').closest('tr').find('td:eq(0)').html();
        var fundId = Number(jQuery('#fund option').filter(function() {return jQuery(this).html() == fundName;}).val());
        jQuery.post('getNavPackReportsStatus', {clientId: clientId, fundId: fundId, periodEndDate: navDate}, function(data) {
            var reports = data;

            for(var count = 0; count< reports.length; count++) {
                jQuery('#wrkbkRptTable tbody').append('<tr>' +
                        '<td><input type="checkbox" id="'+reports[count].reportStoreId+'" name="'+reports[count].reportName+'" checked/></td>'+
                        '<td>' + (count + 1) + '</td>'+
                        '<td>' + reports[count].reportGroupDisplayName + '</td>'+
                        '<td>' + reports[count].reportName + '</td>'+
                        '<td id="chkReportID">' + ((reports[count].reportStoreId == null || reports[count].reportStoreId == '') ? '<font color="red">Not Available</font>' : '<font color="green">Available</font>') + '</td>'+
                        '</tr>');
            }


        });

    });

I tried to disable check box and uncheck check box using this, but it's not working

 jQuery('#wrkbkRptTable input:checked').each(function() {
     var test=jQuery('#wrkbkRptTable input:checked').attr('id');
    if(test==null || test=='' || test=='undefined')
    {
         alert(test);
         jQuery("#"+test+"").prop( "disabled", true );
    }

});    

I want to disable check box and uncheck it using first td (id attribute value) like this: if (id == null then disable & uncheck it)

Upvotes: 0

Views: 1280

Answers (5)

rajesh kakawat
rajesh kakawat

Reputation: 10896

try something like this

$(document).ready(function() {
     jQuery('#wrkbkRptTable input:checked').each(function() {
        var test = this.id;
        if(test==null || test=='undefined' || test.trim()=='')
        {
             this.checked = false;
             this.disabled = true;
        }
    });
});

Upvotes: 0

jason
jason

Reputation: 879

 jQuery('#wrkbkRptTable input:checked').each(function() {
 var test=jQuery(this).attr('id');
  if(!test)
    {
      alert(test);
      jQuery(this).prop( "disabled", true );
     }

  });

Upvotes: 0

user2424727
user2424727

Reputation: 26

I guess the conditions in if are not quite fit

if(test==null || test=='' || test=='undefined')
{
    // means not got test 
}

try this

if(test)
{
    // ur codes 
}

Upvotes: 0

Alex Todef
Alex Todef

Reputation: 370

Try this

jQuery.each( jQuery('#wrkbkRptTable input:checked'), function(_, item) {
  var item = jQuery(item);
  var id = item.attr('id');
  if (typeof id == 'undefined') {
    item.attr('checked', false);
    item.attr('disabled', true);
  }
} )

This code will receive all checked checkboxes. Then it will test if item's ID is present. If not, it will uncheck current checkbox and disable it.

Upvotes: 1

Money Parashar
Money Parashar

Reputation: 56

Use firebug aor chrome debugger to debug this.Your code for disabling checkbox is right.

Upvotes: 0

Related Questions