jQuery - add/append value to "rel" attribute

Alex picture Alex · Apr 8, 2011 · Viewed 17.1k times · Source

I have a set of random links, like this:

<a rel="foo"> ... </a>
...
<a> ... </a>

Some of them may have a rel attribute, some not.

How can add a rel attribute with a value to each link, and if the link already has one, then append my value to the existing value/values?

Also how can I skip any elements with that have a certain rel attribute, like rel="ignore" ?

Answer

karim79 picture karim79 · Apr 8, 2011

Short 'n sweet:

$("a[rel!='ignore']").each(function() {
    this.rel += 'theValue';
});

You can try it here.