views:

44

answers:

3
+1  Q: 

String contains

On my intranet page, I have links which end with .doc or .xls.

How do I use jQuery to check if they end with .xls so I can add a little javascript to those links

EDIT:

This does not work:

$("a.resultLink").live('click', function(event)
{
    // do something here
    // ...

    // change .doc link behaviour
    var anchor = $("a:contains('.doc')"); 
    if (anchor){ 
        event.preventDefault(); 
        alert("this is a doc");
    } 

    // do something else here
    // ...
});
+4  A: 

Use the Attribute Ends With Selector [name$=value]. For example, to add the class excel_link to all links to files ending with .xls, you could do:

$("a[href$='.xls']").addClass("excel_link");

If you've already got the element, then you just want to do this:

$("a.resultLink").live('click', function(event)
{
    // do something here
    // ...

    // change .doc link behaviour
    var anchor = $(this); 
    if (anchor.attr("href").match(/\.doc$/){ 
        event.preventDefault(); 
        alert("this is a doc");
    } 

    // do something else here
    // ...
});
Dominic Rodger
How do I use that in an if statement? if $("a[href$='.xls']") { //do something different }
oshirowanen
@oshirowanen: One infers from the question that you're not taking full advantage of jQuery's expressive power. You can have a function doSomethingDifferent() that does something different to all selectors that match the pattern. You don't need a conditional branch. Don't iterate through your links; instead figure out the conditions or cases and handle them each in turn as sets.
Tim
How do I take advantage of jQuery's expressive power?
oshirowanen
We are typing at the same time; I was editing my comment as you typed your question. I had accidentally hit ENTER before finishing the comment.
Tim
@Tim, I'm not proficient enough with jQuery yet to implement what you've typed.
oshirowanen
Added current script to original question
oshirowanen
@oshirowanen - I've edited my answer to hopefully address what you want to do.
Dominic Rodger
That works. Thanks.
oshirowanen
A: 

If its in the text you can use the CSS locator contains. The documentation is at http://api.jquery.com/contains-selector/

var anchor = $("a:contains('.xls')");
if (anchor){
  anchor.addClass("myNewClass"); // Change this to what every jQuery command that you want
}

if you have a number of these then

$("a:contains('.xls')").each(function(index){
   $(this).addClass("myNewClass");
});
AutomatedTester
I need to change the behaviour of such anchor tags, so how would I use this with an if statment i.e. if ( $("a:contains('.xls')") ) { // do something different with xls files }
oshirowanen
I've tried the following and it affects all links instead of just the .doc links... var anchor = $("a:contains('.doc')"); if (anchor){ event.preventDefault(); alert("this is a doc"); }
oshirowanen
Added current script to original question
oshirowanen
A: 

standard javascript (regex) can do this:

if(str.match(/\.doc$/)) {dosomething();}

where str is the filename to check

Willem
Added current script to original question
oshirowanen