Ed Sinek
Ed Sinek

Reputation: 4977

finding anchors with rel attribute and external value

I'm looking for a jQuery translation of the following:

function externalLinks() {   
 if (!document.getElementsByTagName) return;   
 var anchors = document.getElementsByTagName("a");   
 for (var i=0; i<anchors.length; i++) {   
   var anchor = anchors[i];   
   if (anchor.getAttribute("href") &&   
       anchor.getAttribute("rel") == "external")   
     anchor.target = "_blank";   
 }   
}   
window.onload = externalLinks;

taken from the following site:
http://articles.sitepoint.com/article/standards-compliant-world/3

I started with something like this, but don't know how to search for the rel attribute (without looping through all anchors):

$(document).ready(function () {
    $('body').find('a').findTheRelTagWithExternalValue;
});

Thanks,
--Ed

Upvotes: 3

Views: 2700

Answers (1)

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827396

Something like:

$('a[href][rel=external]').attr('target', '_blank');

Upvotes: 7

Related Questions