This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Child terms unticked by default

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Child terms unticked by default

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #38452
    theapetheape
    Participant

    When a parent category is ticked in a search, the children are ticked too. Is there any way to disable this behaviour, so the parent is ticked, but not the children?

    Image one is what happens by default, image two is the behaviour I would like to happen when the parent is ticked.

    • This topic was modified 3 years, 10 months ago by theapetheape.
    #38458
    theapetheape
    Participant

    To add, I have everything un-checked. The children are ticked when the parent is ticked, I only want the parent ticked when it’s clicked.

    #38471
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    I had to a dig a bit for this, but I recall someone else about a year ago had a similar request, and we found a possible solution with minimal coding.

    Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

    add_action('wp_footer', 'asp_add_menu_stop_checkbox_parent_child');
    function asp_add_menu_stop_checkbox_parent_child() {
      ?>
      <script>
    	jQuery(function($){
    		$('*[data-lvl]').removeAttr('data-lvl');
    	});
      </script>
      <?php
    }

    This should basically remove the attribute from the checkbox elements, which indicates their levels.

    #38472
    theapetheape
    Participant

    Thank you, it does exactly what I asked for. 🙂

    The problem I have now, is there anyway to make the code more specific to those checkboxes?

    It disables/overrides the “Frontend Search Settings > Categories & Taxonomy Terms > Hide child terms, where the parent checkbox is unchecked?” option. And I’m worried it might have other knock on effects on the website.

    #38473
    theapetheape
    Participant

    You cannot access this content.

    #38479
    theapetheape
    Participant

    I’m so close, I don’t know if you can help nudge it over the line.

    	jQuery(function($){
    		$('*[data-lvl]').removeAttr('data-lvl');
    		$(".asp_option_cat_level-1").hide();
    		$(".asp_option_cat_level-2").hide();
    		$(".asp_option_cat_level-0").click(function(){
    			$(this).siblings('.asp_option_cat_level-1').toggle();
    			$(this).siblings('.asp_option_cat_level-2').hide();
    		});
    		$(".asp_option_cat_level-1").click(function(){
    			$(this).siblings('.asp_option_cat_level-2').toggle();
    		});  
    	});

    How do I get this hide/show only the children of the checked parent rather than all of the children when a parent is clicked? I also need to figure out how to have the children automatically unchecked when a parent is unchecked.

    #38481
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    Something like this:

    jQuery(function($){
    	$('*[data-lvl]').removeAttr('data-lvl');
    	$(".asp_option_cat_level-1").hide();
    	$(".asp_option_cat_level-2").hide();
    	$(".asp_option_cat_level-0").click(function(){
    		var $next = $(this).next();
    		console.log(this, $next);
    		while ( $next.hasClass('asp_option_cat_level-1') || $next.hasClass('asp_option_cat_level-2') ) {
    			if ( $next.hasClass('asp_option_cat_level-1') ) {
    				$next.toggle();
    			} else {
    				$next.hide();
    			}
    			$next = $next.next();
    		}
    	});
    	$(".asp_option_cat_level-1").click(function(){
    		var $next = $(this).next();
    		while ( $next.hasClass('asp_option_cat_level-2') ) {
    			$next.toggle();
    			$next = $next.next();
    		}
    	});  
    });
    #38483
    theapetheape
    Participant

    Thank you, that’s brilliant! The only bit I can’t figure out now is how to get the children to uncheck automatically when the parent is unchecked.

    #38487
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You are welcome. That should not be too difficult, try this instead of the previous version:

    jQuery(function($){
    	$('*[data-lvl]').removeAttr('data-lvl');
    	$(".asp_option_cat_level-1").hide();
    	$(".asp_option_cat_level-2").hide();
    	$(".asp_option_cat_level-0").click(function(){
    		var $next = $(this).next(), checked = $(this).find('input[type=checkbox]').prop('checked');
    		while ( $next.hasClass('asp_option_cat_level-1') || $next.hasClass('asp_option_cat_level-2') ) {
    			if ( $next.hasClass('asp_option_cat_level-1') ) {
    				$next.toggle();
    			} else {
    				$next.hide();
    			}
    			if ( !checked ) {
    				$next.find('input[type=checkbox]').prop('checked', false);
    			}
    			$next = $next.next();
    		}
    	});
    	$(".asp_option_cat_level-1").click(function(){
    		var $next = $(this).next(), checked = $(this).find('input[type=checkbox]').prop('checked');
    		while ( $next.hasClass('asp_option_cat_level-2') ) {
    			$next.toggle();
    			if ( !checked ) {
    				$next.find('input[type=checkbox]').prop('checked', false);
    			}
    			$next = $next.next();
    		}
    	});  
    });
    #38490
    theapetheape
    Participant

    You cannot access this content.

    #38491
    theapetheape
    Participant

    You cannot access this content.

    #38493
    theapetheape
    Participant

    You cannot access this content.

    #38497
    theapetheape
    Participant

    For anyone else who runs into this issue, here’s the complete code with the refresh issue solved too.

    add_action('wp_footer', 'asp_add_menu_stop_checkbox_parent_child');
    function asp_add_menu_stop_checkbox_parent_child() {
      ?>
    <script>
    jQuery(document).ready(function($){
    	$(window).load(function(){
    		$('*[data-lvl]').removeAttr('data-lvl');
    		$(".asp_option_cat_level-1").hide();
    		$(".asp_option_cat_level-2").hide();
    		$(".asp_option_cat_level-0").click(function(){
    			var $next = $(this).next(), checked = $(this).find('input[type=checkbox]').prop('checked');
    			while ( $next.hasClass('asp_option_cat_level-1') || $next.hasClass('asp_option_cat_level-2') ) {
    				if ( $next.hasClass('asp_option_cat_level-1') ) {
    					$next.toggle();
    				} else {
    					$next.hide();
    				}
    				if ( !checked ) {
    					$next.find('input[type=checkbox]').prop('checked', false);
    				}
    				$next = $next.next();
    			}
    		});
    		$(".asp_option_cat_level-1").click(function(){
    			var $next = $(this).next(), checked = $(this).find('input[type=checkbox]').prop('checked');
    			while ( $next.hasClass('asp_option_cat_level-2') ) {
    				$next.toggle();
    				if ( !checked ) {
    					$next.find('input[type=checkbox]').prop('checked', false);
    				}
    				$next = $next.next();
    			}
    		}); 
    	});  
    });
    </script>
      <?php
    }
    #38503
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

    #38505
    theapetheape
    Participant

    You cannot access this content.

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.