mardi 19 juillet 2016

Dynamic checkboxes - check by a onclick function

Say I have some checkboxes like below:

<label class="ck-button" id="1">
    <input type="checkbox" name="form-field-checkbox" class="ace chkb">
    <span class="lbl txt" >1</span>japan      
</label>
<label class="ck-button" id="2">
    <input type="checkbox" name="form-field-checkbox" class="ace chkb">
    <span class="lbl txt" >2</span>Uk  
</label>
<label class="ck-button" id="3"> 
    <input type="checkbox" name="form-field-checkbox" class="ace chkb">        
    <span class="lbl txt" >3</span>USA     
</label>

Since I have special usage of this checkbox I have a function to prevent it's default action and use prop() to check it. I am using $(this) here is because I have lot of similar buttons.

$(".ck-button").click(function(event) {
    event.preventDefault();
    $(this).find(".ace .chkb").prop('checked', true);
});

I thought find() allows me to get access to the checkbox and check it but it doesn't work, why?




Aucun commentaire:

Enregistrer un commentaire