Scott
Scott

Reputation: 1872

jQuery - Create element and append to all links, except some

The style of the pages I'm building is to have a little triangle after each link. As such, I've built a little script that appends a <span> after each link.

I'm creating a new span, with a class and appending it to all anchors, but I don't want it do to it in the nav anchors.

Of course, I can just hide the appended spansin css for those I don't want, but surely I can just tweak what I have:

$('<span/>',{
    'class': 'im'
    }).appendTo('a').not(".nav ul > li > a");

The not() part isn't working.

Upvotes: 3

Views: 112

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

It should be

$('<span/>',{
    'class': 'im'
    }).appendTo($('a').not(".nav ul > li > a"));

Demo: Fiddle

Upvotes: 7

LeGEC
LeGEC

Reputation: 51850

The not part should be in the 'a' selector :

... 
.appendTo('a:not(.nav ul > li > a)')

I would suggest that you tag your a anchors somehow, to simplify the work of the selector, for example :

  1. add a navLink class to as in the .nav bar,
  2. then your selector becomes 'a:not(.navLink)', and selecting the nodes will involve less work from jQuery

Upvotes: 1

Related Questions