JavaScript - InnerHtml
Jump to navigation
Jump to search
About
NOTE: This page is a daughter page of: JavaScript
There are several ways to get InnerHtml. Probably all you need to do is call: "element.innerText
", but here's some other stuff I've played with:
Inner Text
/**
* Recursively gets all text content, separated by a string.
* Be warned there may be a slight difference between textContent and innerText.
* @return {string} Text content.
*/
function getTextContentRecursiveFromLeaves(element, sepChar) {
var hasChildren = element.children.length > 0;
var hasTextContent = element.innerText;
var allText = '';
if (hasChildren) {
for (var i = 0; i < element.children.length; i++) {
console.log(i);
var newText = getTextTextRecursiveFromLeaves(
element.children[i], sepChar);
if (allText && newText) { newText += sepChar; }
allText += newText;
}
} else if (hasTextContent) {
allText += element.innerText;
}
return allText;
}
/**
* Gets inner text content by striping away HTML via Regex.
* @return {string} Text content.
*/
function getInnerTextUsingRegex(element, sepChar) {
var allText = element.innerHTML;
var regex = /(<([^>]+)>)/ig;
allText = allText.replace(regex, kSepChar);
return allText;
}
See Also
- JavaScript - other JavaScript examples.