Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:06:05+00:00 2026-06-18T02:06:05+00:00

I’m trying to build a two-level multi-select control using nested ULs and jqueryUI selectable.

  • 0

I’m trying to build a two-level multi-select control using nested ULs and jqueryUI selectable.

Currently, I’m restricting selections to the Child level, but what I really want is to be able to select a Parent LI and have all it’s Child LIs select as well. Going further, I would want to be able to select all Child LIs and have their Parent be selected. Anything less than all Child LIs selected would result in the Parent being un-selected.

The basic markup:

<ul id="theParentList">
    <li>Parent 1
        <ul>
            <li>Child 1.1</li>
            <li>Child 1.2</li>
        </ul>
    </li>
    <li>Parent 2
        <ul>
            <li>Child 2.1</li>
            <li>Child 2.2</li>
        </ul>
    </li>
</ul>

and the current javascript:

$('#theParentList').selectable({filter: 'li li'});

How would I accomplish the first part: selecting a Parent selects all of it’s Children?

Thanks!

UPDATE:
I’ve got it most of this concept working now:
Selecting a Parent selects all of it’s Children;
Deselecting a Child will deselect it’s Parent

What still isn’t working: Selecting all of a Parent’s Children should select the Parent

Here’s what I’ve got, at this point:

Markup:

<ul id="theParentList">
    <li class="level-1">
        <div>Parent 1</div>
        <ul class="level-2">
            <li><div>Child 1.1</div></li>
            <li><div>Child 1.2</div></li>
        </ul>
    </li>
    <li class="level-1"><div>Parent 2</div>
        <ul class="level-2">
            <li><div>Child 2.1</div></li>
            <li><div>Child 2.2</div></li>
        </ul>
    </li>
</ul>

and the js:

    function SelectSelectableElement (selectableContainer, elementsToSelect){
        $(elementsToSelect).not(".ui-selected").addClass("ui-selecting");
    }

    function handleSelected (){};

    function handleSelection (El){
        if( $(El).parent().hasClass('level-1')){
            var ch = $(El).parent().find('.level-2 .ui-selectee');
            SelectSelectableElement('#theParentList', ch);
        }else{
            //this is a level-2 item 
            //if El and all of it's level-2 siblings are selected, then also select their level-1 parent
        }
    };

    function handleUnselected (El){
        if( $(El).parent().hasClass('level-1') ){
            //unselect all of it's children
            $(El).parent().children().each( function(){
                $(this).find('.ui-selectee').removeClass('ui-selected').addClass('ui-unselected');
            });
        }else{
            //this is a level-2 item, so we need to deselect its level-1 parent
            $(El).parents('li.level-1').find(">:first-child").removeClass('ui-selected');
        }
    };

    $('#theParentList').selectable({
        filter: 'li div',
        selecting: function(event, ui){
            handleSelection(ui.selecting);
        },
        selected: function(event, ui) {
            handleSelected(ui.selected);
        },
        unselected: function(event, ui) {
            handleUnselected(ui.unselected);
        }           
    });

Here’s a fiddle: http://jsfiddle.net/JUvTD/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:06:07+00:00Added an answer on June 18, 2026 at 2:06 am

    Posting an answer to my own question, in case anyone else needs help with the same

    Selecting a parent will select all of it’s children
    Selecting all of the children will select their parent
    UnSelecting a parent will unselect all of it’s children
    Unselecting a child will also unselect it’s parent

    here’s a working fiddle:
    http://jsfiddle.net/QvqCE/1/

    and the javascript

        $('#theParentList').selectable({
            filter: 'li div',
            selecting: function (event, ui) {
                if ($(ui.selecting).parent().hasClass('level-1')) {
                    //this is a level-1 item, so select all of it's children
                    var ch = $(ui.selecting).parent().find('.level-2 .ui-selectee');
                    $(ch).not(".ui-selected").addClass("ui-selecting");
                } else {
                    //this is a level-2 item, so check to see if all of it's siblings are also selected
                    var sibs = $(ui.selecting).parent().siblings().find('.ui-selectee');
                    var notSelected = 0;
                    for (var i = 0; i < sibs.length; i++) {
                        if ($(sibs[i]).hasClass('ui-selected') || $(sibs[i]).hasClass('ui-selecting')) {
                            notSelected = notSelected
                        } else {
                            notSelected = notSelected + 1;
                        }
                    }
                    if (notSelected === 0) { //all siblings are selected, so select their level-1 parent as well
                        $(ui.selecting).parent().parent().parent().find('>:first-child').not(".ui-selected").addClass("ui-selecting");
                    }
                    //otherwise, just select as usual
                }
            },
            unselected: function (event, ui) {
                if ($(ui.unselected).parent().hasClass('level-1')) {
                    //unselect all of it's children
                    $(ui.unselected).parent().children().each(function () {
                        $(this).find('.ui-selectee').removeClass('ui-selected').addClass('ui-unselected');
                    });
                } else {
                    //this is a level-2 item, so we need to deselect its level-1 parent
                    $(ui.unselected).parents('li.level-1').find(">:first-child").removeClass('ui-selected');
                }
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.