'How to search nth child alone in jstree

I want to search only 3th child in jstree. I tried $('#userlogs li>a ').find('span:third-child').jstree('search', searchstring) - this is not working. In my code inside li anchor tab is there. In that anchor tab 4 spans r there. I want to search only 3rd span. Please help me to resolve this issue

Code:

    $(' #userLogs ').jstree({
    'search': {
    case_insensitive: true,
    show_only_matches : true,
    show_only_matches_children: true,
    fuzzy: false,

            },
            'plugins': ['search']
        }).on('search.jstree', function (nodes, str, res) {
                if (str.nodes.length === 0 && searchString.length !=0 ) {
                        $('#userLogs').jstree(true).hide_all();
                        $('#userLogs').append('<p id="searchFound">'+" No search Found "+'</p>');
                 }
        });

    $("#searchTerm").keyup(function ()  {
    searchString = $(this).val();
    $('#userLogs').jstree(true).show_all();
    //$('#userLogs li > a').find('span:nth-child(3)').jstree('search', searchString);
    $('#userLogs li > a').find('span:third-child(3)').jstree('search', searchString);

    }

I can't send my html code here. inside li tab 4 spans are there. While searching anchor tab is automatically getting added. I want to search only 3rd span content.



Solution 1:[1]

You have two problems here:

$('#userLogs').jstree(true).show_all();
//$('#userLogs li > a').find('span:nth-child(3)').jstree('search', searchString);
$('#userLogs li > a').find('span:third-child(3)').jstree('search', searchString);

Your commented line was closer, because there is no such selector as :third-child, but you also want span:nth-of-type(3) rather than :nth-child(3), as nth-child looks for any child.

But the bigger problem is that the jstree method works on a jstree object, not on some random element. Assuming (and it's a big assumption!) that that third span you're looking for contains the node ID, this should work:

var jstree = $('#userLogs').jstree(true)
jstree.show_all()
var nodeId = $('#userLogs li > a').find('span:nth-of-type(3)')
jstree.jstree('search', searchString, inside=nodeId.html())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1