views:

33

answers:

3

Hi folks

I've got an anchor tag with a specific URL to which I want to add a class. I'm currently doing this as follows:

$('a[href=http://this.ismyurl.com/folder/file.html]').addClass('red');

The problem I have is that this URL is not consistent. The filename, file.html is always the same, so if I could manipulate this string and focus on the last 9 characters (for instance) then I could add a class in the same way. The problem is, I'm not sure how to do this.

Can anyone help?

Thanks
Ronnie

A: 
var url = "http://someurl.com"
$('a[href='+url+']').addClass('red');
Mike
Thanks for this Mike, however I'm not sure this would work in my case. I need it to be based on the HTML file, as there are other links that would have the same base URL, to which I don't want to add a class.
rnnbrwn
+4  A: 

You're looking for the attribute end with selector $=

$('a[href$="file.html"]').addClass('red');
Yi Jiang
Perfect, thanks Yi Jiang!
rnnbrwn
+2  A: 

You can use a wildcard in your jQuery selector. I believe this will work:

$('a[href$=/file.html]').addClass('red');

That will add the 'red' class to all links with an href ending in '/file.html'.

Aaron