I am trying to get the inner text of HTML string, using a JS function(the string is passed as an argument). Here is the code:
function extractContent(value) {
var content_holder = "";
for(var i=0;i<value.length;i++) {
if(value.charAt(i) === '>') {
continue;
while(value.charAt(i) != '<') {
content_holder += value.charAt(i);
}
}
}
console.log(content_holder);
}
extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>");
The problem is that nothing gets printed on the console(content_holder stays empty). I think the problem is caused by the "===" operator..
Create an element, store the HTML in it, and get its
textContent
:Here's a version that allows you to have spaces between nodes, although you'd probably want that for block-level elements only:
You could temporarily write it out to a block level element that is positioned off the page .. some thing like this:
HTML:
JavaScript:
One line (more precisely, one statement) version:
you need array to hold values
Try This:-
textContext is a very good technique for achieving desired results but sometimes we don't want to load DOM. So simple workaround will be following regular expression: