0

I'm not understanding something here. This gives an error "undefined is not a function" for line 5. I want to add a class to every link that has ".mp3" but does not have ".php".

jQuery(document).ready(function(){ jQuery('a[href$=".mp3"]') .each(function() { if (this.toString().indexOf(".php")<0) { $(this).addClass('sm2_button');} }); var btn = jQuery('<span class="btn-pressplay">&nbsp;</span>'); jQuery('.sm2_button').prepend(btn) });

Edit: I also tried the following per @user3659034. The error I get is Type error: undefined is not a function console.log produces "a#page-top" so looks like I'm trying to evaluate a weird a on my page that I need to exclude. I got the typeof code from here.

jQuery(document).ready(function(){ //edit NEB //jQuery('a[href$=".mp3"]').addClass('sm2_button'); jQuery("a").each(function(idx) { console.log (this); var attr = $(this).attr('href'); if (typeof attr !== typeof undefined && attr !== false) { if (jQuery(this).attr('href').indexOf(".mp3") > -1) { if (jQuery(this).attr('href').indexOf(".php") == -1) { jQuery(this).addClass('sm2_button'); } } } }); var btn = jQuery('<span class="btn-pressplay">&nbsp;</span>'); jQuery('.sm2_button').prepend(btn) }); This is for the pressplay-lite wordpress plugin to limit changing mp3 links that are downloads instead of mp3s.

Community
  • 1
  • 1
Nathan
  • 67
  • 8

2 Answers2

0

Use jQuery .each() for anchor tag a and get it href attribute's value then check either exist .php or .mp3 in there.

$("a").each(function(idx) {
    if ($(this).attr('href').indexOf(".mp3")  >  -1) {
        $(this).addClass('sm2_button');
    }
});
MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

What you need to do is to use attribute selectors to say select elements with href ending with .mp3(attribute ends with selector - $=) but ignore items having .php in the href attribute(attribute contains selector - *=) like

jQuery('a[href$=".mp3"]').not('[href*=".php"]').addClass('sm2_button');
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531