|
-
Dec 17th, 2008, 03:39 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Remove orphan text from HTML
Suppose I wanted to remove any and all text from a web page that did not reside in a specified tag (either a <p> or <h1> tag).
What would be the way to do it?
-MizPippz
-
Jan 27th, 2009, 05:22 PM
#2
Re: Remove orphan text from HTML
Hi there Ms.Longstocking,
I have just seen this old unanswered post of yours. 
Just in case you still require a solution, here it is...
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<style type="text/css">
div {color:#900;}
h1,p {color:#000;}
h2 {color:#909;}
span {color:#009;}
textarea {color:#090;}
.hide{display:none;}
</style>
<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',removeElementsText,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',removeElementsText);
}
}
function removeElementsText(){
els=document.body.getElementsByTagName('*');
document.getElementById('but').onclick=function() {
for(c=0;c<els.length;c++) {
if((els[c].tagName!='P')&&(els[c].tagName!='H1')) {
for(d=0;d<els[c].childNodes.length;d++){
if(els[c].childNodes[d].nodeType==3){
els[c].childNodes[d].nodeValue='';
}
}
}
}
document.getElementById('but').className='hide';
}
}
</script>
</head>
<body>
<div>
this text is within a div element
<h1>this text is within an h1 element</h1>
this text is within a div element
<h2>this text is within an h2 element</h2>
this text is within a div element
<p>
this text is within a p element
<span>this text is within a span element</span>
this text is within a p element
</p>
this text is within a div element
<textarea rows="3" cols="30">this text is within a textarea element </textarea>
<button id="but">remove text</button>
</div>
</body>
</html>
~ the original bald headed old fart ~
-
Jan 27th, 2009, 05:25 PM
#3
Thread Starter
Hyperactive Member
Re: Remove orphan text from HTML
Thanks Coothead.
I had already resolved this issue a while back. However, this code will be very useful for another project I am working on.
Cheers,
MizPippz
-
Jan 27th, 2009, 05:30 PM
#4
Re: [RESOLVED] Remove orphan text from HTML
No problem, you're very welcome.  p.s. Have you seen my possible solution to this post of yours...
~ the original bald headed old fart ~
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|