views:

90

answers:

3

I have the following line of code which works well:

$("a.grouped_elements[href$=.jpg],a.grouped_elements[href$=.png],a.grouped_elements[href$=.gif]").fancybox();

Problem is, if the href is .JPG it doesn't work it only works with .jpg. How can I make the above case insensitive so either all caps or no caps or a mix all match for the file extension?

Thanks

+4  A: 

Apparently, "a filter function should do what you need".

Since I am not familiar with filter functions, I would try something like the following, though I am not sure how efficient/inefficient it might be compared to filter functions mentioned above:

$('a.grouped_elements').each(function(){
    var elem = $(this);
    if(elem.attr('href').match(/(gif|jpg|png)$/i) != null) {
        elem.fancybox();
    }
});

(hasn't been tested but that's the general gist of it)

Ricket
Interesting but this isn't giving an error and isn't working..?
AnApprentice
Follow standard debugging techniques... It looks fine to me.
Ricket
Right but this code applies fancybox to each item individually versus all at the same time. Isn't that a huge performance hit?
AnApprentice
Ok first error was that grouped_elements was missing an e. But even after fixing that it's still not working at the IF - Is this right? match('/(gif|jpg|png)$/i') != null) I don't know regex?
AnApprentice
You don't need the quotes for regexes. This should suffice: `if (elem.attr('href').match(/(gif|jpg|png)$/i)) { ... }`
harto
Fixed the typo, removed the quotes. Also regarding applying fancybox to each item: one way or another, fancybox is applied to each item in the set. jQuery just expands it in the function for convenience so that you don't have to have loops all over your code. I could be wrong, but I'd say there is probably no performance difference; I don't see how the internals could be any different than a loop like this one, looping over the matched elements that ".fancybox()" is called on.
Ricket
And I verified harto's comment; match() doesn't seem to work with a string parameter but it works as fixed, without the quotes. Also see my comment on your answer.
Ricket
+1  A: 

Using filter as also mentioned by @Ricket. The idea is to return true if we want the element to be included, false otherwise.

var fancyBoxable = $('a.groupd_elements').filter(function(){
    var href = $(this).attr('href') || '';
    return /(gif|jpg|png)$/i.test(href);
});

fancyBoxable.fancybox()
Anurag
This doesn't work
AnApprentice
it works. i can create an example on jsfiddle with exactly this code, but I'd rather let you show me why it doesn't work for you first.
Anurag
A: 

Ok this ended up working:

$('a.grouped_elements').each(function(){
    var elem = $(this);
    // Convert everything to lower case to match smart
    if(elem.attr('href').toLowerCase().match('/gif|jpg|png/') != null) {
        elem.fancybox();
    }
});
AnApprentice
You should award the correct answer to Ricket, since he suggested this solution for you.
harto
Also without the $, the regular expression will match gif|jpg|png anywhere in the string. $ means end of the string.
Ricket