The standard ":contains()" selector matches the contents of the element in a case sensitive manner. But it is limited and there have been many times that I've needed to exactly match the contents of an element, so I threw together these two useful selectors.
In this demo, you can see the difference when you see what "td:contains(2)" matches - doing so will match all table cells that contain a "2" including dates that begin and end with "2" and the event titles that contains a "2".
":containsExact()" will grab the innerHTML from the element (the date span in this case) and exactly match it. I made this selector case-insensitve so you won't have to worry about the text case.
":containsExactCase()" will grab the innerHTML from the element (the event span in this case) and exactly match the contents in a case-sensitive manner. Try both "Vacation" and "vacation" in the demo.
":containsRegex()" will grab the innerHTML from the element (also the event span in this case) and use regex to match the contents in either a case-sensitive or insensitive manner (depending on the regex "i" flag).
To include these selectors, just add the following code outside of any $(document).ready(function(){...})
$.extend($.expr[':'],{
containsExact: function(a,i,m){
return $.trim(a.innerHTML.toLowerCase()) === m[3].toLowerCase();
},
containsExactCase: function(a,i,m){
return $.trim(a.innerHTML) === m[3];
},
containsRegex: function(a,i,m){
var regreg = /^\/((?:\\\/|[^\/])+)\/([mig]{0,3})$/,
reg = regreg.exec(m[3]);
return RegExp(reg[1], reg[2]).test($.trim(a.innerHTML));
}
});
Updated the script by changing the "$(a).text()" into "a.innerHTML" to prevent triggering on nested text.
ReplyDeleteGood extension. Thanks a lot.
ReplyDeleteThanks. Like it a lot.
ReplyDeleteawesome. I wrote a demo using this that will keep growing into an interesting plugin.
ReplyDeletehttp://jsfiddle.net/kneebreaker/jYHNL/
This is great!
Like!
ReplyDelete