1

I want to change style of an element which contains a specified word in href.

jquery

jQuery('a:contains("afyon")').css('color': '#fff');

html

<div class="saglink"><a title="bayan adana" href="http://www.site.co/adana-bayan/">Adana bayan</a></div>
<div class="saglink"><a title="bayan adiyaman" href="http://www.site.co/adiyaman-bayan/">Adıyaman bayan</a></div>
<div class="saglink"><a title="bayan afyon" href="http://www.site.co/afyon-bayan/">Afyon bayan</a></div>
<div class="saglink"><a title="bayan aksaray" href="http://www.site.co/aksaray-bayan/">Aksaray bayan</a></div>

What is the correct way to do that ?

user198989
  • 4,574
  • 19
  • 66
  • 95

2 Answers2

5

You need to use attribute contains selector:

jQuery('a[href*=afyon]').css('color', '#fff');
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
2

You could use filter() and return all elements whose href value contains a specific word. You should also convert the string to lower case in order to get all possible matches.

Example Here

$('a').filter(function(){
    return $(this).prop('href').toLowerCase().indexOf('afyon') > -1;
}).css('color', '#fff');
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304