Home >Web Front-end >JS Tutorial >jQuery method to get node and sub-node text_jquery
For the html snippet below,
<div id="text_test">test text<a href="techbrood.com" rel="external nofollow" >techbrood co.</a></div>
Get node plain text:
var text = $('#text_test').text()
This will get "test text techbrood co.", which means the text of all nodes (including child nodes) of the current element will be read out.
If you only want to get the text of the main node, the method is more complicated:
var text = $("#text_test").contents().filter(function() { return this.nodeType === 3; }).text();
Get the text of a child node:
var text = $("#text_test > a").first().contents().filter(function() { return this.nodeType === 3; }).text();