Supposed that I want to add extra content from my div (Demo).
Is there any way to get the text with javascript excluding the different elements in a scope?
Let's use the same example with some improvisations as Get the pure text without HTML element by javascript? for simplicity sake.
function get_content(){
var a = txt.innerText || txt.textContent;
console.log('From #txt:', a, '\n-------\nI want to exclude the following:');
var classA=document.getElementsByClassName('A');
console.log(classA[0].textContent);
var classB=document.getElementsByClassName('B');
console.log(classB[0].textContent);
var classC=document.getElementsByClassName('C');
console.log(classC[0].textContent);
}
<input type="button" onclick="get_content()" value="Get Content"/>
<p id='txt'> Ronald.
<span class="A">I am</span>
<span class="B">working in </span>
<span class="C">ABC company.</span>
</p>
And suppose that I want to separate how to get the content from the element id
excluding the span
's content? Which is in the example was "Ronald".
Update:
I'm considering the following method to achieve them:
var classAlt=document.getElementsByClassName('Alt');
/*If class contains same character with #txt won't work:*/
console.log(a.substr(0, a.indexOf(classAlt[0].textContent.charAt(0))));
/*Haven't found the disadvantage of the following:*/
console.log(txt.childNodes[0].wholeText.trim());
function get_content(){
var a = txt.innerText || txt.textContent;
console.log('From #txt:', a, '\n-------\nI want to exclude the following:');
var classA=document.getElementsByClassName('A');
console.log(classA[0].textContent);
var classB=document.getElementsByClassName('B');
console.log(classB[0].textContent);
var classC=document.getElementsByClassName('C');
console.log(classC[0].textContent);
/*I'm thinking of the following: */
var classAlt=document.getElementsByClassName('Alt');
/*If class contains same character with #txt won't work:*/
console.log(a.substr(0, a.indexOf(classAlt[0].textContent.charAt(0))));
/*Haven't found the disadvantage of the following:*/
console.log(txt.childNodes[0].wholeText.trim());
}
<input type="button" onclick="get_content()" value="Get Content"/>
<p id='txt'> Ronald.
<span class="A">I am</span>
<span class="B">working in </span>
<span class="C">ABC company.</span>
<span class="Alt">alternative which won't work.</span>
</p>