krrr25
krrr25

Reputation: 679

How to delete list-divider li once delete all of its child li

I am having jquery mobile listview with data-split-icon "delete". In that list view i am using data-divider to separate the li with different groups. I want to delete the particular data-divider once all li are removed from the listview under that data-divider. Say Example: Check the following listview code snippet.

<ul data-role="listview" data-autodividers="true" data-theme="c" data-divider-theme="d" data-split-icon="delete" data-split-theme="c">
    <li id="group1" data-role="list-divider">Group 1</li>
    <li id="item1">
        <a href="animals.html" data-rel="dialog" class="ui-link-inherit">Cat</a>
        <a id="del_4" data-theme="c"></a>
    </li>
    <li id="item2">
        <a href="animals.html" data-rel="dialog" class="ui-link-inherit">Dog</a>
        <a id="del_5"></a>
    </li>

    <li id="group2" data-role="list-divider">Group 2</li>
    <li id="item1">
        <a href="animals.html" data-rel="dialog" class="ui-link-inherit">Fish</a>
        <a id="del_4" data-theme="c"></a>
    </li>
</ul>

In the above code, if I delete li "Fish", I want to remove the "Group 2" list-divider li also automatically, since there is no "li" present under this group. I am try to delete separately "li" but i can't remove the list-divider "li" once all the child "li" removed.

Upvotes: 1

Views: 431

Answers (1)

Amit
Amit

Reputation: 196

First Get Group id.

   var p_id=$(this).parent().parent().attr("id");

then check its child

  var childDivs = $("#"p_id li);

Remove if it do not contain child.

   if(childDivs.length<1)
         $("#"p_id).remove();

Upvotes: 1

Related Questions