vendredi 29 mai 2015

check all jquery not working for dynamically populated checkbox

I have a jquery function to select all checkboxes , which are populated in PHP loop. The output of the loop is similar to below:

<label class="check"><input type="checkbox" name="selectall" value="All"/> Select All</label>
<label class="col-md-3 col-xs-12 control-label">Types Of function</label>
<div class="col-md-6 col-xs-12">                                                                                            
    <div class="col-md-7">                                    
        <label class="check"><input type="checkbox"  name="chkUser" class="icheckbox"/> Birthday</label>
    </div>
    <div class="col-md-7">                                    
        <label class="check"><input type="checkbox"  name="chkUser" class="icheckbox"/> Family Function</label>                   
    </div>
    <div class="col-md-7">                                    
        <label class="check"><input type="checkbox"  name="chkUser" class="icheckbox"/> conference</label>                        
    </div>
    <div class="col-md-7">                                    
        <label class="check"><input type="checkbox"  name="chkUser" class="icheckbox"/> wedding</label>                           
    </div>
    <div class="col-md-7">                                    
        <label class="check"><input type="checkbox"  name="chkUser" class="icheckbox"/> Corporate</label>                             
    </div>
</div>

And the jquery function is :

$('.selectall').click(function() { //alert('HHH');
    if ($(this).is(':checked')) {
        $('.icheckbox').attr('checked', 'checked');
    } else {
        $('.icheckbox').attr('checked', false);
    }
});

But unfortunately its not working ! What's wrong ?

Jsfiddle Here




Aucun commentaire:

Enregistrer un commentaire