Question

I have this code in jQuery, that I want to reimplement with the prototype library.

// make external links open in popups
// this will apply a window.open() behaviour to all anchor links
// the not() functions filter iteratively filter out http://www.foo.com 
// and http://foo.com so they don't trigger off the pop-ups

jQuery("a[href='http://']").
      not("a[href^='http://www.foo.com']").
      not("a[href^='http://foo.com']").
      addClass('external');

jQuery("a.external").
      not('a[rel="lightbox"]').click( function() {
      window.open( jQuery(this).attr('href') );
      return false;
});

How can you iteratively filter an collection of elements using an equivalent to the not() operators listed here in jQuery?

Was it helpful?

Solution

The filtering can be done using the reject method like so:

$$('a').reject(function(element) { return element.getAttribute("href").match(/http:\/\/(www.|)foo.com/); }).invoke("addClassName", "external");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top