JavaScript get textContent excluding children

First, I’m creating a library for JavaScript and I can not use jQuery. I’m trying to get the text content of an HTML element without the text contents of its children.

Both attributes innerText and textContent don’t give me what needed, please help.

Answer

You can solve using DOM API as childNodes and nodeType.

var elChildNode = document.querySelector("#hello").childNodes;
var sChildTextSum = "";

elChildNode.forEach(function(value){
    if(value.nodeType === Node.TEXT_NODE) { 
        console.log("Current textNode value is : ", value.nodeValue.trim());
        sChildTextSum += value.nodeValue;
    }
}); 

console.log("All text value of firstChild : ", sChildTextSum);

I created a sample code as above.

https://jsfiddle.net/nigayo/p7t9bdc3/