1

Just a quick one. What am I doing wrong here?

$("#content a:not(target=_blank)").live("click", function(){
   alert("You clicked an internal link!!")
})

Cheers :)

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Alex Fox
  • 1,175
  • 2
  • 15
  • 27

5 Answers5

9

You can use the attribute not equals selector:

$("#content a[target!='_blank']").live("click", function(){
   alert("You clicked an internal link!!");
});
Dennis
  • 32,200
  • 11
  • 64
  • 79
5

the jQuery :not() selector accepts selectors inside of it, so try something like this:

$('#content a:not([target="_blank"])').live("click", function(){
   alert("You clicked an internal link!!")
})
Aaron Adams
  • 1,657
  • 16
  • 23
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • +1 for the technically correct answer in terms of CSS selectors, which makes this potentially a few microseconds faster than jQuery's attribute-not-equals selector. – BoltClock Aug 19 '11 at 21:11
  • I generally don't care about micro-optimization, let's just say I sleep soundly at night even if my script will take 10ms longer then it could have :) – Madara's Ghost Aug 19 '11 at 21:12
  • 1
    Neither do I, actually; I purposely used the word "microseconds" to troll anyone who does. – BoltClock Aug 19 '11 at 21:23
  • Ah damn! thanks! Sometimes I find the jQuery documentation a little lacking! – Alex Fox Aug 19 '11 at 21:37
1

brackets?

$("#content a:not([target=_blank])").live("click", function(){
   alert("You clicked an internal link!!") 
})
Litek
  • 4,888
  • 1
  • 24
  • 28
  • @BoltClock: That is a relatively new requirement for jQuery or Sizzle or whatever's handling the selectors, is it not? – Wesley Murch Aug 19 '11 at 21:15
  • @Litek: Ah yeah, I was just talking about CSS selectors in another answer. If it gets processed as a CSS selector then the quotes aren't needed. – BoltClock Aug 19 '11 at 21:23
  • Hmm yeah it does work, I specifically remember a build of jQuery breaking on unquoted values, perhaps it was 1.6.0 or 1.6.1, looks like that was changed for the most recent version? Or am I going crazy and that never happened? – Wesley Murch Aug 19 '11 at 21:29
1

You're missing the square brackets for the attribute selector, and quotes around the value.

$('#content a:not([target="_blank"])')

Single quotes would work here as well.

$("#content a:not([target='_blank'])")

I believe it was version 1.6.0 where attribute quotes became mandatory, in previous versions it was optional, and this broke a lot of my code wen I updated. It seems 1.6.2 or 1.6.1 may have removed this requirement, although the docs say they are still mandatory. Better to do what they say in case backwards support gets removed in a future version.

As a CSS selector however, it is still optional for the most part, so at least get used to using them in jQuery from now on if you haven't already.

Community
  • 1
  • 1
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • I used to use them in attribute selectors purely because they were required in XHTML/XML attributes. Also, quotes are pretty :P – BoltClock Aug 19 '11 at 21:24
  • I kind of like how the "naked" values look in stylesheets, I don't use quotes around `url()` sources either, it's not necessary so why bother? Optional is optional. – Wesley Murch Aug 19 '11 at 21:26
  • There are some edge cases where they're required in attributes, though. Albeit, edge cases. http://stackoverflow.com/questions/5578845/css-attribute-selectors-the-rules-on-quotes-or-none – BoltClock Aug 19 '11 at 21:32
  • I'd say that using a url or non-alphanum as a value in an attribute selector is not an edge case, I think it's quite relevant. Never knew that and I suspect it's not common knowledge - thanks for showing me that link, probably saved me some head-scratching at some point in the future. I probably would have tried escaping before I thought of quoting. – Wesley Murch Aug 19 '11 at 21:36
1

Try this:

$("#content a:not([target='_blank'])").live("click",your_function);
josemota
  • 974
  • 1
  • 8
  • 27