ra********@hotmail.com (Raja Kannan) wrote in message news:<5b**************************@posting.google. com>...
Is there a way to remove text portion from the HTML keeping the HTML
Tags using the browser, say javascript RegEx or something ?
I have seen lot of examples removing HTML tags to get the text but how
the reverse of it ?
any sample code or any suggestion would be appreciated.
Here is a way of changing the text. I set the text to blank. This
leaves the html intact. You will notice when all the text is set to
blank, the paragraph remains. Maybe you should remove the text node.
The problem is complicated by the fact you can have imbeded html tags
in the text and you cannot directly address the text element. You can
see how I addressed the paragraph element then scanned for the text.
I am new at this type of coding, so there may be a better way. I
tested this in netscape 7.1 under macos x.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>change document text</title>
<SCRIPT type="text/javascript">
function changeData(node)
{
alert("node.nodeType = " + node.nodeType +
" node.tagName = " + node.tagName);
removeCharacters(node);
}
function removeCharacters(n)
{
if (n.nodeType == 3 &&
n.parentNode.tagName != "SCRIPT") // Check if n is a 'real' Text
object
{
alert("n.nodeType = " + n.nodeType +
" n.tagName = " + n.tagName);
var theParent = n.parentNode;
alert("theParent.nodeType = " + theParent.nodeType +
" theParent.tagName = " + theParent.tagName);
alert("length of text node = " + n.length +
" '" + escape(n.data) + "'");
n.data = " ";
return;
}
// Otherwise, n may have children whose characters we need to
traverse
var numChars = 0;
for (var m = n.firstChild; m != null; m = m.nextSibling)
{
alert("another. n.nodeType = " + n.nodeType +
" n.tagName = " + n.tagName);
removeCharacters(m);
}
return;
}
</script>
</head>
<body onload="alert('onload complete');
changeData(document.getElementById('p1'));">
<p id="p1">This <i>is</i> paragragh #1.</p>
<p id="p2">This is paragragh #2.</p>
</body>
</html>
Robert