Last active
March 24, 2026 14:10
-
-
Save barneycarroll/981147 to your computer and use it in GitHub Desktop.
`:notext` is an alternative to the `:empty` selector for jQuery. Elements consisting exclusively of an amount of whitespace or HTML comments will be returned.
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
| /* | |
| jQuery's ':empty' selector only returns true if the element contains no text node, or whose text node consists exclusively of zero or more spaces and tabs. This ':notext' selector will return true for elements whose text nodes can also contain line breaks (any whitespace character) and HTML comments. | |
| */ | |
| $.expr[':'].notext = function detectNoText(x){ | |
| return x.innerHTML && x.innerHTML.replace(/(<!--.*(?!-->))|\s+/g, '').length === 0 | |
| } |
Apologies for bothering anyone that comes here, but if you know how one might include this in a jquery based userscript I would be most thankful! 😲
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A must-have in jQuery! Thanks for the code.