SO-37412469

by David Thomas

HTML

<span id="count">255<span>words</span></span>

JavaScript

//
function getChildTextOf (n) {
  let text = Array.from( n.childNodes ).filter( n => n.nodeType === 3 ).map(n => n.nodeValue);
  
  return text && text.length === 1 ? text.shift() : text;
}

console.log(getChildTextOf(document.getElementById('count')));