I am using the code below to replace text inside a div. But I need to loop through a specific div, instead of everything on the page, that is the div’s text nodes…
I just do not understand how to refer to the div in the code below.
(function (parent) {
var childs = parent.childNodes;
// if there are children to this
if (childs && childs.length) {
// loop through each text node
for (var i = 0, node; node = childs[i]; i++) {
More Code as requested:
function npup(parent) {
var children = parent.childNodes, child;
for (var idx=0, len=children.length; idx<len; ++idx) {
child = children.item(idx);
alert(child);
if (child.nodeType===3) {
// it is a text node. do magic.
for (var x = 0; x < en_count; x++) {
// what is the current content of the current node
var value = content(child);
// get current english phrase
var from = en_lang[x];
// get current other language phrase
var to = other_lang[x];
if (typeof(from) != 'undefined' && value.match(from)) {
content(node, value.replace(from, to));
}
}
}
}
}
var theDiv = document.getElementById(‘mydiv’);
npup(theDiv);
Edit Oh, I misunderstood your question apparently. Here is how to loop throught the text nodes of some element:
I wrote this in plain javascript since your code example was plain js. The selector engines of Libraries like Prototype and jQuery for convenience usually ignores the text nodes on retrieval so one doesn’t have to bother with them. OR get to them, depending on how you lok at it.