Reputation: 3444
Having some issues of looping over elements and adding class based on their href. The class seems to be applying to all elements even if those elements aren't having the substring in their href.
HTML:
<ul>
<li><a href="#special-link">link 1</a></li>
<li><a href="/normallink1">link 2</a></li>
<li><a href="/normallink2">link 3</a></li>
</ul>
JQUERY:
$('a').each(function() {
var _this = $(this);
if(_this.attr('href').substr('special')!==0){
_this.addClass('special_link');
}
});
I have also set up a jsfiddle. Any apologies if this might be a duplicate, I did have a look around though before posting the question.
Thanks.
Upvotes: 0
Views: 37
Reputation: 553
No need for each loop
$("a[href*='special']").addClass('special_link');
Upvotes: 3
Reputation: 1368
This should work for you,
$('a').each(function() {
var _this = $(this);
if(_this.attr('href').indexOf('special') > -1){
_this.addClass('special_link');
}
});
Upvotes: 2