Created
July 29, 2012 01:54
-
-
Save TALlama/3195685 to your computer and use it in GitHub Desktop.
A jQuery extension to return only the visible text in a node.
This file contains 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.fn.visibleText = function() { | |
if (this.length === 0) return null; | |
if (this.length > 1) return this.first().visibleText(); | |
if (this[0].nodeType === 3) return this.text(); | |
var buffer = []; | |
$.each(this.contents(), function(ix, e) { | |
if ($(e).is(':visible')) { | |
return buffer.push($(e).visibleText()); | |
} | |
}); | |
buffer.join(''); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your function seems to have quite a few problems. Wouldn't something like this work better?