[Javascript] styling text

Scott Petersen petersen at ma.medias.ne.jp
Sat Dec 1 01:53:01 CST 2007


> Have you tried using innerHTML?  As in
>
> span.innerHTML = span.innerHTML.replace("dirty", "<span  
> class='blueWord'>dirty</span");
>
> It may not be elegant enough for some, but it seems to do the job.
I could only get this to work in Safari 3. It didn't work in Firefox  
or Opera. I'm not sure why.
I tried the following, which did work. It is even more inelegant.  
This only takes care of the first occurrence of the word.

function init(){
	var list=document.getElementById('theList');
	var theItems=list.getElementsByTagName('li');
	for (i=0;i<theItems.length; i++){
		var oneItem=theItems[i].getElementsByTagName('span');
		var headWord=oneItem[0].firstChild.nodeValue;
		var sentence=oneItem[2].firstChild.nodeValue;
		oneItem[2].innerHTML=sentence.replace(headWord, headWord.bold 
().fontcolor("blue"));
		}
}

At any rate, thank you.

Scott Petersen

> From: Scott Petersen petersen at ma.medias.ne.jp
>
> I have a problem I can't seem to solve. I want to take this markup:
> dirty ?? I have to clean my room. It is dirty.
> and color the word "dirty" in the sentence blue, or set it off in  
> some other way. I thought to create a span element and style it  
> with CSS. However, I am not able to replace the plain text with the  
> span element. I could mark the word up to begin with, but for work  
> flow I thought I might try to let javascript do the work. Any  
> thoughts? Scott Petersen By the way, the Japanese is merely a gloss  
> of the English.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20071201/f20211b1/attachment.htm>


More information about the Javascript mailing list