Replace Text with html in js

by bRIMOs

HTML

<div>
  Apple dsqd sqd qsd Apple sqd sqd Apple
  <p>Apple is an apple and it's an apple </p>
  <p><span>Apple also is an PeanApple</span></p>
  <div>
     what you think about <p>
     Apple now !
     </p> 
     
  </div>
  
  
</div>

CSS

.red {
  color:red;
  font-weight:bold;
}

JavaScript

walk(document.body);

function walk(node)  
{

	  var child, next;
		switch ( node.nodeType )  
    {
    
        case 1: 
            handleText(node);
            break;
        case 9:  
        case 11: 
            child = node.firstChild;
            while ( child ) 
            {
                next = child.nextSibling; 
                walk(child);
                child = next;
            }
            break;

        case 3: 
            break;
    }
}

function handleText(node) 
{
    var v = node.innerHTML;
    v = v.replace(/Apple/gi, '<span class="red">Pineapple</span>');
		node.innerHTML = v ;
}