Using jQuery to find list items containing specific text in nested lists

user1572121

I am working on setting up a way to select or filter a list using radio groups of "modifiers" or attributes. The lists I'm filtering can be multi-level nested ul-li.

My eventual goal is to have only the intended list items with the correct attribute or text show, the others would be hidden.

In my example, I'm having issues with selecting only what I want. It's selecting the whole nested ul if there is an li that contains the text I'm looking for.

Here's a jsFiddle I made

$('.refine').on('change', 'input[type="radio"]', function() {

    var sortattr = $(this).val();   

    $(".mylist li:contains('" + sortattr + "')").each(function() {
        $(this).addClass('here');
    });
});

<form class="refine" role="form">
    <input type="radio" value="left" name="modifiers" id="reflatleft">Left
    <input type="radio" value="right" name="modifiers" id="reflatright">Right
    <input type="radio" value="unspecified" name="modifiers" id="reflatunspecifed">Unspecified
</form>                         

<ul class="mylist">
    <li>item misc header
      <ul>
        <li>item left</li>
        <li>item right</li>
        <li>item left</li>
        <li>item unspecified</li>
        <li>item somethings else</li>
      </ul>
    </li>
    <li>item left</li>
    <li>item right</li>
    <li>item left</li>
    <li>item unspecified</li>
    <li>item somethings else</li>
</ul>
Sagi_Avinash_Varma

working solution

You just have to check whether there are any child elements in the <li>'s to be highlighted so just adding a if construct would solve the problem

$('.refine').on('change', 'input[type="radio"]', function() {

    var sortattr = $(this).val();   

    $(".mylist li:contains('" + sortattr + "')").each(function() {
        if($(this).children().length == 0){
            $(this).addClass('here');
        }
    });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count the number of lists containing specific element in a nested list

From Dev

Find similar items in list of lists using Python

From Dev

Using jQuery to filter for specific nested unordered lists

From Dev

Combine items in a list until an item containing specific text is found?

From Dev

Find specific table row containing substring text of a jquery variable of html

From Dev

Find specific table row containing substring text of a jquery variable of html

From Dev

Find a table containing specific text

From Dev

Find a table containing specific text

From Dev

Summation of specific items in a nested list

From Dev

Find empty lists in nested list of lists

From Dev

Python: Iterate over each item in nested-list-of-lists and replace specific items

From Dev

Selenium : Find nested div with specific plain text using xpath

From Dev

Converting list items to dictionary using lists items

From Dev

How to find specific object in nested lists?

From Dev

Find common items in list of lists of strings

From Dev

Find common items in list of lists of strings

From Dev

find lists that start with items from another list

From Dev

Find first and last row containing specific text

From Dev

How to find the number of nested lists in a list?

From Dev

jQuery: Find all DIVs containing specific elements

From Dev

Find duplicate items in all lists of a list of lists and remove them

From Dev

How to transform a list of dictionaries, containing nested lists into a pandas df

From Dev

Find a specific text in tr td jquery using contains

From Dev

Find a specific text in tr td jquery using contains

From Dev

Find lists in a list that contains two specific elements

From Dev

Using map to find the average of nested lists

From Dev

jQuery Find the specific text in the page

From Dev

Lists with multiple 'nested' items

From Dev

jQuery nested sortable with editable list items

Related Related

  1. 1

    Count the number of lists containing specific element in a nested list

  2. 2

    Find similar items in list of lists using Python

  3. 3

    Using jQuery to filter for specific nested unordered lists

  4. 4

    Combine items in a list until an item containing specific text is found?

  5. 5

    Find specific table row containing substring text of a jquery variable of html

  6. 6

    Find specific table row containing substring text of a jquery variable of html

  7. 7

    Find a table containing specific text

  8. 8

    Find a table containing specific text

  9. 9

    Summation of specific items in a nested list

  10. 10

    Find empty lists in nested list of lists

  11. 11

    Python: Iterate over each item in nested-list-of-lists and replace specific items

  12. 12

    Selenium : Find nested div with specific plain text using xpath

  13. 13

    Converting list items to dictionary using lists items

  14. 14

    How to find specific object in nested lists?

  15. 15

    Find common items in list of lists of strings

  16. 16

    Find common items in list of lists of strings

  17. 17

    find lists that start with items from another list

  18. 18

    Find first and last row containing specific text

  19. 19

    How to find the number of nested lists in a list?

  20. 20

    jQuery: Find all DIVs containing specific elements

  21. 21

    Find duplicate items in all lists of a list of lists and remove them

  22. 22

    How to transform a list of dictionaries, containing nested lists into a pandas df

  23. 23

    Find a specific text in tr td jquery using contains

  24. 24

    Find a specific text in tr td jquery using contains

  25. 25

    Find lists in a list that contains two specific elements

  26. 26

    Using map to find the average of nested lists

  27. 27

    jQuery Find the specific text in the page

  28. 28

    Lists with multiple 'nested' items

  29. 29

    jQuery nested sortable with editable list items

HotTag

Archive