I'm trying to apply a class to all elements which contain text or which contain text and other dom elements inside. At least there must be some sort of text inside the element.
This div should get the class:
<div class="theClass">Some text</div>
This div should get the class too:
<div class="theClass">Some text<div class="theClass more">More text</div></div>
But this one should only give the child div the class:
<div class=""><div class="theClass more">More text</div></div>
At the moment i'm using the :hasText selector which i found here finding elements with text using jQuery.
jQuery.expr[':'].hasText = function(element, index) {
// if there is only one child, and it is a text node
if (element.childNodes.length == 1 && element.firstChild.nodeType == 3) {
return jQuery.trim(element.innerHTML).length > 0;
}
return false;
};
But it only returns elements which contain text. I like to have elements which contain text and other elements (if there are any).
Thanks!