Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Last active January 14, 2024 07:57
Show Gist options
  • Save barneycarroll/981147 to your computer and use it in GitHub Desktop.
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.
/*
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
}
@pegasuspect
Copy link

A must-have in jQuery! Thanks for the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment