How do I loop through elements inside a div?
Posted
by crosenblum
on Stack Overflow
See other posts from Stack Overflow
or by crosenblum
Published on 2010-04-20T17:09:35Z
Indexed on
2010/04/20
17:13 UTC
Read the original article
Hit count: 241
I have to make a custom function for search/replace text, because firefox counts text nodes differently than IE, Google Chrome, etc..
I am trying to use this code, that I saw at Firefox WhiteSpace Issue since in my other function, I am looping numerically through nodes, which serves my functional needs perfectly, in other browsers.
But refuses to work, as part of a search/replace function that takes place after some ajax content is loaded.
Here is the code, that I have tried to get to work, but I must be missing the correct understanding of the context of how to loop thru elements inside a div.
// get all childnodes inside div
function div_translate(divid) {
// list child nodes of parent
if (divid != null) {
// var children = parent.childNodes, child;
var parentNode = divid;
// start loop thru child nodes
for(var node=parentNode.firstChild;node!=null;node=node.nextSibling){
// begin check nodeType
if(node.nodeType == 1){
// get value of this node
var value = content(node);
// get class of this node
var myclass = node.attr('class');
console.log(myclass);
// begin check if value undefined
if (typeof(value) != 'undefined' && value != null) {
console.log(value);
// it is a text node. do magic.
for (var x = en_count; x > 0; x--) {
// get current english phrase
var from = en_lang[x];
// get current other language phrase
var to = other_lang[x];
if (value.match(from)) {
content(node, value.replace(from, to));
}
}
}
// end check if value undefined
}
// end check nodeType
}
// end loop thru child nodes
}
}
© Stack Overflow or respective owner