oliverh72
oliverh72

Reputation: 55

Search data attribute of element with Jquery, with partial matches

I'm building a filter of items, using an input field and the on("keyup") event. It looks like this:

$("#inputFilter").on("keyup", filterPrograms);

It's working well to find items within a class, like this:

<h6 class="programName">Science</h6>

However, within some of those H6s, I've added a data attribute, like so:

<h6 class="programName" data-tag="indigenous interdisciplinary ">Aboriginal Studies</h6>

How can I modify the following code to filter both the class's text (currently working), and also the contents of the data-tag? This simply hides the parent block '.mix' whenever a partial match isn't true. Here's my function:

   function filterPrograms(event) {
        // Retrieve the input field text
        var filter = $('#inputFilter').val();
        // Loop through the blocks
        $(".programName").each(function(){
            // this part isn't working!!
            dataResult = $(this).is('[data-tag*='+filter+']') < 0;
            // If the item does not contain the text phrase, hide it
            textResult = $(this).text().search(new RegExp(filter, "i")) < 0;
            if (textResult || dataResult) {
                $(this).closest(".mix").hide();           
            } else {
                $(this).closest(".mix").show();
            }
        });
    }

Now, I'm pretty sure that it's because the .is() will never fully match, which is why I need a partial match. In the above example, typing "indi" should provide a positive result against the contents of the data-tag attribute; this doesn't work. Typing "abo" matches against textResult, and works fine.

I know I'm missing something, but reading the documentation (and SO) on this isn't helping. Thanks in advance.

Edit: here's the working function with @Triptych's solution:

$(".programName").each(function(){
    // If the item does not contain the text phrase hide it
    dataResult = $(this).is('[data-tag*="'+filter+'"]');
    textResult = $(this).text().search(new RegExp(filter, "i")) < 0;
    if (textResult && !dataResult) {
        $(this).closest(".mix").hide(); // Hide the item if there are no matches
    } else {
        $(this).closest(".mix").show(); // Show the item if there are matches
    }
});

Upvotes: 4

Views: 2331

Answers (1)

Kenan Banks
Kenan Banks

Reputation: 212010

Well for one thing, you can't compare the result of .is() to 0 that way. is() returns a boolean.

So change this.

    dataResult = $(this).is('[data-tag*='+filter+']') < 0;

To this.

    dataResult = $(this).is('[data-tag*="'+filter+'"]');

Note that I also quoted the string for the attribute match, which will allow the query to include spaces.

Upvotes: 3

Related Questions