jeudi 22 octobre 2015

Radio buttons group jquery click event

i have 4 radio buttons hidden under each image inside each div.

   <div class="temp-inner" id="someid">
        <?php while($row = mysql_fetch_array($res)){?>
            <div data-id="<?=$row['id'];?>" class="hand  temp-selector">
                <input type="radio" name="templates_id" class="templates_id" value="<?=$row['id'];?>" />
                <label class="temp-type" style="background-image:url(<?=$row['src'];?>)"></label>
            </div>
        <? } ?>
   </div>

i need to make radio button under image = checked, so i decided to create onClick function

$(".hand").on("click", function() {
    $(this).children("input:radio[name=templates_id]").attr('checked',true); 
}

this part works perfect. Now i want to add new class to whole if radio button is checked under it. i thought that i can use if - else statement, but it doesn't work.

$(".hand").on("click", function() {
    $(this).children("input:radio[name=templates_id]").attr('checked',true); 

    if($("input:radio[name=templates_id]").is(":checked")) {
        $(".hand").addClass("active");
    } else {
        $(".hand").removeClass("active");
    } 
};

How can i write it correctly? Thx a lot, best regards!

Aucun commentaire:

Enregistrer un commentaire