SO - 21249558
by Arun P Johny
HTML
<div>
<div id="t1">Text1</div>
<div id="t2">Text2
<ul id="t3">
<li id="t4">Text3</li>
</ul>
</div>
</div>
JavaScript
function getText(el) {
return $(el).contents().map(function () {
return this.nodeType == 3 && $.trim(this.nodeValue) ? $.trim(this.nodeValue) : undefined;
}).get().join('')
}
$('div *').each(function () {
console.log(this.id, getText(this))
})