1

i want to remove text that is between any HTML tags :

example :

<div>
   <h1>Title</h1>
</div>

my var result should be :

<div>
    <h1></h1>
</div>
5
  • 6
    stackoverflow.com/questions/1732348/… Commented Jan 6, 2014 at 17:36
  • 6
    Use the DOM, seriously. Commented Jan 6, 2014 at 17:36
  • 1
    If you want to modify live tags on a page, just set their textContent empty. Commented Jan 6, 2014 at 17:42
  • loop through all the childern of body tag and $(this).empty() its cotent Commented Jan 6, 2014 at 17:42
  • 1
    blanking the textContent or using empty() on everything could destroy sub-nodes... Commented Jan 6, 2014 at 17:52

6 Answers 6

6

If, as your question suggests, you want to remove all text from between any HTML tags… only the real DOM is going to cut it.

function removeAllTextNodes(node) {
    if (node.nodeType === 3) {
        node.parentNode.removeChild(node);
    } else if (node.childNodes) {
        for (var i = node.childNodes.length; i--;) {
            removeAllTextNodes(node.childNodes[i]);
        }
    }
}

This, unlike textContent and innerHTML, will keep all existing element structure in place and remove only text.

If you really have a string and are using client-side JavaScript in a browser, and the string represents part of a document’s content (and not an entire document – i.e. you won’t find any DTD, <html>, <head>, or <body> elements within), then you can parse it just by putting it into an element:

var container = document.createElement("div");
container.innerHTML = htmlString;
removeAllTextNodes(container);
return container.innerHTML;

Otherwise, you’ll probably want an HTML parser for JavaScript. Regular expressions, as it’s been noted, aren’t great at parsing HTML.

Sign up to request clarification or add additional context in comments.

Comments

4

VANILLA JS TO THE RESCUE

var x = document.getElementsByTagName("h1");
for (var i=0; i<x.length; i++) {
    x[i].innerHTML = "";
}

Just insert any tag you'd like and wallah, no need for regex, or a 90kb library.

Comments

3

Javascript is already able to accomplish this with built in functions in a way that in conceptually superior to regex

<div>
   <h1 id="foo">Title</h1>
</div>
<script>
   document.getElementById("foo").textContent = ""
</script>

1 Comment

innerText is non-standard and will not work in browsers like Firefox. It's an IE extension that chrome supports to work better on IE sites. Maybe you meant .innerHTML or .textContent ?
2

You would probably want to do something like this;

var elements = document.getElementsByTagName('*');
for(var i = 0; i < elements.length; i++) {
    var element = elements[i];
    if(element.children.length === 0) {
        elements[i].textContent = '';
    }
}

This

  • Finds all elements
  • Loops through them
  • Removes any text content

Docs:

You can also make this re-usable like so

var removeAllText = function() {
    var elements = document.getElementsByTagName('*');
    for(var i = 0; i < elements.length; i++) {
        var element = elements[i];
        if(element.children.length === 0) {
            elements[i].textContent = '';
        }
    }
}

Then whenever you want you can do this

removeAllText();

5 Comments

Would this be inside the function nukeThisPage()?
Yeah, or inside if (!jQuery) { nukeThisPageYouHater() } ;)
This is pretty pointless with a *; it’s equivalent to document.documentElement.removeChild(document.head); document.documentElement.removeChild(document.body);, usually.
@minitech how about now?
I forgot for a second that removing the text removes the elements too :)
0

Don't use regex. Use something like loadXMLDoc() to parse the DOM and print the tags, instead of trying to remove the values from within the tags.

1 Comment

HTML is not valid XML.
0

Tested i JS and work for me:

String.replace(/<yourtag>[\s\S]*<\/yourtag>/g, ""); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.