Macsupport
Macsupport

Reputation: 5444

adding to an href

I want to add to an href when a link becomes "active" My code selects a specific class, adds a class and then, if it is a specific file, needs this additional input ("?lightbox[width]=350&lightbox[height]=170") added to the href. My code is as follows:

 $('.active').removeClass("lightbox").addClass("lightbox");

$('.active  [href$='.swf'],[href$='.mov'],[href$='.flv'],[href$='.wmv']').attr("href", function(i, href) 
{return href + '?lightbox[width]=350&lightbox[height]=170';

Doesn't want to add to the href. Any help would be appreciated!

Upvotes: 0

Views: 320

Answers (1)

Šime Vidas
Šime Vidas

Reputation: 185873

Try this:

$(".active")
  .filter("[href$='.swf'],[href$='.mov'],[href$='.flv'],[href$='.wmv']")
  .attr("href", function(i, href) {
    return href + '?lightbox[width]=350&lightbox[height]=170';
  });

I changed the outer quotes to double because you are already using single quotes inside the selector. If you want to have quotes inside a string, than you have to make sure that they are not of the same type as the ones that encapsulate the string:

"This 'string' is OK"
'This "one" also'

I also changed the code to first select all ".active" elements, and then filter out the one's which href attribute doesn't end with the desired strings (.swf, .mov, .flv or .wmv). The way your code was working was that the ".active" selector applied only to the first [href] selector.

Another thing...

Since the dot character is a meta-character in a selector, you may try to escape it.

Instead of this:

.filter("[href$='.swf'],[href$='.mov'],[href$='.flv'],[href$='.wmv']")

try this:

.filter("[href$='\\.swf'],[href$='\\.mov'],[href$='\\.flv'],[href$='\\.wmv']")

Update!

I did some testing and the \\ escape does not work but the \ escape does. Also, in my browser - Chrome on Win7 - it works even without escaping.

Upvotes: 4

Related Questions