Skip to content Skip to sidebar Skip to footer

Prototype: Select Element Containing Text (as Jquery :contains())

Using Prototype I have to select and hide all
  • elements containing the word Foo inside:
  • Solution 1:

    var lis = document.getElementsByTagName("li");
    [].forEach.call(lis, function (li) {
      if (li.textContent.indexOf("Foo") > -1) {
        li.style.display = "none";
      }
    });
    

    Seriously, you don't need libraries for this. Just a good bit of DOM4 & ES5 does it for you.

    Use the normal shims if you care about legacy platform support

    SideNote: don't ever do $("li:contains('Foo')") its epicly slow as hell. Not to mention murderous abuse of selectors4

    Solution 2:

    :contains does work in Prototype, you just need to remember that the output of $$ is an array.

    $$('li:contains("Foo")').invoke('hide');
    
  • Post a Comment for "Prototype: Select Element Containing Text (as Jquery :contains())"