-1

I have a function that selects all <a> with href that contains sun:

jQuery('a[href*="sun"]').each(function(i, el) {
    alert(el);
});

How to make it case insensitive (contains substring sUN or sun or SuN etc)? Checked Case-insensitive attribute-value selector with Jquery but with no luck for this question.

Community
  • 1
  • 1
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212
  • 1
    *"Checked [Case-insensitive attribute-value selector with Jquery](http://stackoverflow.com/questions/5755722/case-insensitive-attribute-value-selector-with-jquery) but with no luck for this question."* Huh? The accepted answer there is **exactly** how you do this. – T.J. Crowder Jan 28 '14 at 15:05
  • That answer checks for equality, you should use [`.indexOf()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) method after `.toLowerCase()`. – Ram Jan 28 '14 at 15:11
  • Would anybody help to write it in one string instead of asking for closing this question? Thanks. – Haradzieniec Jan 28 '14 at 15:17

2 Answers2

1

You might need to use a custom filter:

$('a').filter(function () {
    return $(this).attr('href').toLowerCase().indexOf('sun') >-1;
})
Bic
  • 3,141
  • 18
  • 29
0

And here is my answer on my question:

$('a[href]').filter(function() {return this.href.toLowerCase().indexOf("sun") >=0 ;}).each(function(i, el) {
    alert(el);
});
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212
  • Still unclear about possible duplicate if the answers are really different - there were no "filter" and "each" all together. – Haradzieniec Jan 28 '14 at 15:36