Last active
February 7, 2018 16:58
-
-
Save seemly/359911583d91911ce25244be1cfddffd to your computer and use it in GitHub Desktop.
jQuery - Find object containing direct textNode content
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 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