How to isolate text nodes in jQuery?
Hi,
Suppose I have a HTML element that looks like this.
<div>This is <span>a text node</span>. There are more <span>letters in this one.</span> Test up.</div>
I wish to identify and isolate "This", "is", ".", "There", "are", "more", "Test", and "up." as text nodes.
Is there a way to do this through jQuery, either natively or through a jQuery plugin?
Using the .contents() method, I get an array of several texts, but they're not completely isolated.
<script type="text/javascript">
$(document).ready(function(){
var myContent = $("div").contents().filter(function() { return this.nodeType == 3; });
console.log(myContent);
});
</script>
will give me this in myContent:
[<TextNode textContent="This is ">, <TextNode textContent=". There are more ">, <TextNode textContent=" Test up.">]
Thanks for reading.