Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make the checkbox working properly in following scenario?
    primarykey
    data
    text
    <p>I'm using PHP, Smarty and jQuery for my website. In a smarty template I'm having few checkboxes upon which I'm working. For your reference following is my smarty code snippet regarding checkboxes.</p> <pre><code>&lt;table cellpadding="5" cellspacing="0" border="0" id="groups_listing" style="{if $data.show_group=='on'}display:{else}display:none;{/if}"&gt; &lt;tr&gt; {if $all_groups} {assign var='i' value=0} {foreach from=$all_groups item="group"} {if $i%4 == 0}&lt;/tr&gt;&lt;tr&gt;{/if} &lt;td valign="top" align="left" width="200"&gt; &lt;table cellpadding="5" cellspacing="0" border="0"&gt; &lt;tr&gt; &lt;td&gt; &lt;input type="checkbox" name="parent_groups[]" id="{$group.parent_id}" id="{$group.parent_id}" onchange="check_subgroups(this.id, 'class_{$group.parent_id}');" value="{$group.parent_id}" {foreach from=$user_groups item=user_grp} {if $user_grp== $group.parent_id} checked="checked" {/if}{/foreach}&gt; &lt;label&gt;&lt;strong&gt;{$group.parent_name} &lt;/strong&gt;&lt;/label&gt; &lt;input type="hidden" name="main_groups[]" id="main_groups[]" value="{$group.parent_id}"&gt; &lt;/td&gt; &lt;/tr&gt; {foreach from=$group.subgroups item=subgroup} &lt;tr&gt; &lt;td&gt; &lt;input type="checkbox" name="groups_{$group.parent_id}[]" class="class_{$group.parent_id}" onchange="uncheck_main_group('{$group.parent_id}'); return false;" value="{$subgroup.group_id}" style="margin-left:20px;" {foreach from=$user_groups item=user_grp} {$user_grp} {if $user_grp==$subgroup.group_id} checked="checked" {/if}{/foreach}&gt; &lt;label&gt;{$subgroup.group_name}&lt;/label&gt; &lt;/td&gt; &lt;/tr&gt; {/foreach} &lt;/table&gt; {assign var='i' value=$i+1} {/foreach} {/if} &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>The javascript functions are as follows:</p> <pre><code>function show_groups(check_box_id) { if ($(check_box_id).is(':checked')) { $('#groups_listing').show(); } else { $('#groups_listing').hide(); } } function check_subgroups(parent_id, class_name) { var chk = document.getElementById(parent_id).checked; if(chk == true) $('.'+jQuery.trim(class_name)).attr('checked', true); else $('.'+jQuery.trim(class_name)).attr('checked', false); } </code></pre> <p>The first javascript function named <strong>show_groups()</strong> works fine, noissue with that. My issue is with the second function named <strong>check_subgroups()</strong>. It also works fine initially after page load but later if I uncheck and again chech the parent checkbox it's not working. Also when I check all the child checkboxes one bu one then it is expected that the parent child box should automatically get checked and vice versa. This is not working in current situation. Can you help me in achieving this functionality. For your reference I'm attaching the screen shot of checkboxes with this question. You can see here in the question I've checked all the child check boxes under parent <strong>Course</strong> but the parent checkbox is not getting checked automatically. Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload