5

Possible Duplicate:
jQuery: Select <a> which href contains some string

I am working on a little twitter implementation for a website. The javascript I use exports the code as following:

<ul>
 <li>twitter message here <a href="http://twitter.com/username/ID">time of twitter</a></li>
</ul>

Now I like to add a class to the twitter link, so I can give it a different css then normal links inside the tweets.

Anyone got any thoughts how to fix this in a proper way? :)

Community
  • 1
  • 1
oceanmountain
  • 121
  • 2
  • 9
  • 1
    http://stackoverflow.com/q/303956/106261 – NimChimpsky Oct 03 '11 at 11:44
  • This question is about "starts with" not "contains'. The solution to the other would be `[href*='...']`or `[href$='...']` or so, but for this one is `[href^='...']`. – Meligy Oct 03 '11 at 23:46

2 Answers2

13
$('a[href^="http://twitter.com"]').addClass('twitter');

http://jsfiddle.net/apjD6/

davin
  • 44,863
  • 9
  • 78
  • 78
  • Your answer here also explained why my code wasn't working. I had space between the A and the [href.... – Volomike Apr 20 '12 at 19:31
2
$(function() {
  $("a[href^='http://twitter.com/']").addClass("someClass");
});

Check reference here:
http://api.jquery.com/attribute-starts-with-selector/

Meligy
  • 35,654
  • 11
  • 85
  • 109