Created
October 7, 2011 15:24
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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