Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping thru text

Status
Not open for further replies.

MarkJ2

Technical User
Sep 25, 2001
17
0
0
US
I am writing a script that will loop thru the elements in a page and place enclosing <STRONG> </STRONG> tags around all text inside all P elements.

I have the loop written
function start(pText)
for ( var loop = 0; loop < document.all.length; ++loop )

but am not sure how to write the next lines of code. I have

if ( document.all[ loop ].tagName = P )
{
elements += id.innerText = <STRONG>;
}
//to write changed text to screen.
pText.innerText += elements;

Also I have several paragraphs to pass to the start function, which the for loop and the next lines of code are in. Can each paragraph have the same ID tag (i.e. ID = pText)and then be passed to the start function like so:
start(pText).
If not, how do I go about passing several
paragraphs to a function?

Thanks for the help.
 
So you're saying you want
Code:
<p>this is a paragraph</p>
to be
Code:
<p><strong>this is a paragraph</strong></p>
right?

Try running it through this function (assuming lowercase <p>):
Code:
function replace(haystack, oldNeedle, newNeedle) {
	i = haystack.indexOf(oldNeedle);
	r = &quot;&quot;;
	if (i == -1) return haystack;
	r += haystack.substring(0,i) + newNeedle;
	if (i + oldNeedle.length < haystack.length)
		r += replace(haystack.substring(i + oldNeedle.length, haystack.length), oldNeedle, newNeedle);
	return r;
	}
Code:
replace(lineInFile, &quot;<p>&quot;, &quot;<p style='font-weight:bold'>&quot;);
The function is a good one to replace one substring with another. The replacement string is the same as the one you're after:
Code:
<p style='font-weight:bold'>this is a paragraph</p>
Hope that helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top