Skip to content

Instantly share code, notes, and snippets.

@AbraaoAlves
Created October 7, 2011 15:24
Show Gist options
  • Save AbraaoAlves/1270514 to your computer and use it in GitHub Desktop.
Save AbraaoAlves/1270514 to your computer and use it in GitHub Desktop.
Custom jQuery Selector to tag that contains text(ignore accents). It works equals :contains selector, but select all tags inpedendent of accents
(function($) {
var accent_map = {
'á':'a',
'à':'a',
'â':'a',
'å':'a',
'ä':'a',
'a':'a',
'ã':'a',
'ç':'c',
'é':'e',
'è':'e',
'ê':'e',
'ë':'e',
'í':'i',
'ì':'i',
'î':'i',
'ï':'i',
'ñ':'n',
'ó':'o',
'ò':'o',
'ô':'o',
'ö':'o',
'õ':'o',
'ú':'u',
'ù':'u',
'û':'u',
'ü':'u',};
String.prototype.replaceEspecialChars = function() {
var ret = '', s = this.toString();
if (!s) { return ''; }
for (var i=0; i<s.length; i++) {
ret += accent_map[s.charAt(i)] || s.charAt(i);
}
return ret;
};
String.prototype.contains = function(otherString) {
return this.toString().indexOf(otherString) !== -1;
};
$.extend($.expr[':'], {
'contains-IgnoreAccents' : function(elemt, idx, math) {
var expression1 = math[3].toLowerCase(),
semAcent1 = expression1.replaceEspecialChars(),
expression2 = elemt.innerHTML.toLowerCase(),
semAcent2 = expression2.replaceEspecialChars();
return semAcent2.contains(semAcent1);
}
});
})(jQuery);
///using this:
/// $(":contains-IgnoreAccents('josé')")
///results: 'jose' and 'josé'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment