Hooch
Hooch

Reputation: 29713

jquery multiply contatins

I want to represent that in jquery:

    var menuItems = document.getElementsByTagName("li");
for (var k = 0; k < menuItems.length; k++)
{
    if (menuItems[k].className == "menu")
    {
        var child = menuItems[k].firstChild;
        if (child.className == "menulink")
        {
            var tempSum = 0;
            tempSum += child.href.indexOf("bwshop");
            tempSum += child.href.indexOf("report");
            tempSum += child.href.indexOf("tutorial");
            tempSum += child.href.indexOf("chat");

            if (tempSum > 0) child.style.display = "none";
        }
    }
}

This is my fail not working code:

$("li.menu > a.menulink").filter(':contains("bwshop"), :contains("report"), :contains("tutorial"), :contains("chat")').hide();

Upvotes: 1

Views: 81

Answers (2)

David Hellsing
David Hellsing

Reputation: 108530

$("li.menu > a.menulink").filter(function() {
    return /(bwshop|report|tutorial|chat)/.test(this.href);
}).hide();

Upvotes: 1

Clive
Clive

Reputation: 36965

The :contains selector reads the inner text of the tag whereas your original code is searching in the href attribute.

You could change your code to use different selectors:

$("li.menu > a.menulink").filter('[href*="report"], [href*="tutorial"], [href*="chat"]').hide();

Upvotes: 4

Related Questions