Skip Navigation

[Resolved] select the parent if user select the child JS

This thread is resolved. Here is a description of the problem and solution.

Problem:
select the parent if user select the child JS

Solution:
You need to add custom javascript code as shared with the following reply to check parent checkbox automatically when you select child checkbox.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/select-the-parent-if-user-select-the-child-js/#post-1324331

Relevant Documentation:

This support ticket is created 5 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by JakubV7709 5 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1324237

Hi guys, is it possible to setup JS code for automaticlly select the parent if user select the child? I found this and im trying to parse it to my form without succes. It will be helful for another users of toolset.

jQuery(document).ready(function($){
  synchronize_child_and_parent_category($);
});
 
function synchronize_child_and_parent_category($) {
  $("input[name='vyrobce[]']").find('input').each(function(index, input) {
    $(input).bind('change', function() {
      var checkbox = $(this);
      var is_checked = $(checkbox).is(':checked');
      if(is_checked) {
        $(checkbox).parents('li').children('label').children('input').attr('checked', 'checked');
      } else {
        $(checkbox).parentsUntil('ul').find('input').removeAttr('checked');
      }
    });
  });
}

I want to use for my taxanomy:

<div class="form-group col-sm-4">
<label>Choose vendor:</label>
<div class="vyrobceform">
[cred_field field='vyrobce' force_type='taxonomy' output='bootstrap' display='checkbox' class='vyrobceform']
[cred_field field="vyrobce_add_new" taxonomy="vyrobce" type="add_new"]
</div></div>

Thank you

#1324275

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've added the following code to your form's JS box:

jQuery(document).ready(function($){
  
  id = jQuery('[name="vyrobce[]"]').attr("id");
  
   $("input[name='vyrobce[]']").each(function(index, input) {
     
      $(this).on("change",function(){
         var checkbox = $(this);
        var is_checked = $(checkbox).is(':checked');
        var parent = $(this).data("parent");
         if(is_checked) {   
          $('input[value="'+parent+'"').prop('checked', true);
         }else{
            $('input[value="'+parent+'"').prop('checked', false);
         }
    });
  
  });

});

It will automatically check/uncheck the parent based on the checkbox state.

#1324279
Untitled.png

Hi Minesh,

it works almost perfect, but is there a way how to unselect parent ONLY if all of the child is unselect? Let me explain it better:

1) User select child of parent - it will select parent perfectly!
2) User select second child of parent - it will keep selecting of parent (its ok).
3) User unselect one of the child from parent - it will unselect parent :-/ - i just need to keep unselect parent when the one of child is selected

I need to unselect parent of child only if user will unselect all of the child of parent.

#1324331

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've adjusted the code again as given under:

jQuery(document).ready(function($){
  
  id = jQuery('[name="vyrobce[]"]').attr("id");
  
   $("input[name='vyrobce[]']").each(function(index, input) {
     
      $(this).on("change",function(){
         var checkbox = $(this);
        var is_checked = $(checkbox).is(':checked');
        var parent = $(this).data("parent");
         if(is_checked) {   
          $('input[value="'+parent+'"').prop('checked', true);
         }else{

           var count =  $("input[name='vyrobce[]']:checked").length - 1;
          
           if(count==0){
            	$('input[value="'+parent+'"').prop('checked', false);
           }
         }
    });
  
  });

});

I hope its working as expected now 🙂

#1324339

My issue is resolved now. Thank you!