You can cycle through each of the nodes, checking their nodeType property in turn and updating the nodeValue property with "0" if node is the text node (indicated by nodeType == 3 ).
Assuming you have this HTML :
<div onclick="doReplace(this)"> 1<b>2</b>3<b>4</b>5 </div>
Then you can write a simple replacement function that calls itself recursively, for example:
window.doReplace = function (rootNode) { var children = rootNode.childNodes; for(var i = 0; i < children.length; i++) { var aChild = children[i]; if(aChild.nodeType == 3) { aChild.nodeValue = '0'; } else { doReplace(aChild); } } }
A working script can be found here: http://jsfiddle.net/p9YCn/1/
source share