473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

removing text from HTML but keeping HTML intact

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.
Jul 23 '05 #1
2 2680
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
Jul 23 '05 #2
DU
Raja Kannan wrote:
Is there a way to remove text portion from the HTML keeping the HTML
Tags using the browser, say javascript RegEx or something ?
Remove text portion? Could be either of these W3C DOM 2 Character Data
methods:

deleteData(offset, count)

or

replaceData(offset, count, arg in DOMString)
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.


I'm not sure I understand what you're asking. But you'll find plenty of
interactive demo examples in these 2 pages which will work in any/all
modern browsers (MSIE 6, Mozilla 1.x, NS 6.2+, Opera 7.x, K-meleon 0.8+,
Galeon 1.2, Safari 1.x, Konqueror 3.x, etc.):

DOM level 2 CharacterData Interface tests
http://www10.brinkster.com/doctorunc...acterData.html

innerHTML versus nodeValue performance comparison
http://www10.brinkster.com/doctorunc...NodeValue.html

DU
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dariusz | last post by:
I am having problems with trying to remove some tags using PHP. What I want is to perform a search and replace on a URL, so if someone tried to put a URL into a guestbook database, instead of the...
15
by: Brent W. Hughes | last post by:
When doing object-oriented stuff, it bothers me to have to type "self" so many times. I propose that Python allow the programmer to optionally type ".variable" instead of "self.variable" to mean...
6
by: bruce | last post by:
hi... i'm running into a problem where i'm seeing non-ascii chars in the parsing i'm doing. in looking through various docs, i can't find functions to remove/restrict strings to valid ascii...
6
by: Markus Ernst | last post by:
Hi Searching for a possibility to display some text with preserved white space and line breaks, but with long lines being wrapped, I found this CSS declaration, which I found helpful: pre {...
1
by: vbfoobar | last post by:
Hello, I have HTML input to which I apply some changes. Feature 1: ======= I want to tranform all the text, but if the text is inside an "a href" tag, I want to leave the text as it is. ...
3
by: Dekortage | last post by:
Hi all... I am able to grab the text that a user has selected on a web page, using this code: function moreInfo() { if (!isIE) { var t = window.getSelection(); // act on variable "t";
10
by: apparker | last post by:
I'm creating a new GUI for a program and it is for a medical exam. There are so many different things to ask someone during a history it wastes too much space to make checkboxes for everything so I...
3
by: sebzzz | last post by:
Hi, I'm doing a little script with the help of the BeautifulSoup HTML parser and uTidyLib (HTML Tidy warper for python). Essentially what it does is fetch all the html files in a given...
1
by: littlealex | last post by:
IE6 not displaying text correctly - IE 7 & Firefox 3 are fine! Need some help with this as fairly new to CSS! In IE6 the text for the following page doesn't display properly - rather than being...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.