Skip to content

Instantly share code, notes, and snippets.

@seemly
Last active February 7, 2018 16:58
Show Gist options
  • Save seemly/359911583d91911ce25244be1cfddffd to your computer and use it in GitHub Desktop.
Save seemly/359911583d91911ce25244be1cfddffd to your computer and use it in GitHub Desktop.
jQuery - Find object containing direct textNode content
function getTextNodes(cssSelector, findString) {
var items = $(cssSelector)
.contents()
.filter(function() {
var isText = this.nodeType == Node.TEXT_NODE;
if (isText) {
var text = $.trim(this.textContent);
if (text.length && text.indexOf(findString) > -1) {
return true;
}
}
return false;
});
return items.length ? items : false;
}
var items = getTextNodes(".test p", "5");
if (items) {
items.each(function() {
$(this.parentNode).addClass("yup");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment