views:

147

answers:

1

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);

+1  A: 

Edit Oh, I misunderstood your question apparently. Here is how to loop throught the text nodes of some element:

function npup(parent) {
    var children = parent.childNodes, child;
    for (var idx=0, len=children.length; idx<len; ++idx) {
      child = children.item(idx);
      if (child.nodeType===3) {
        // it is a text node. do magic.
      }
    }
}

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.

npup
Do we have to loop through all text nodes to find the ones just inside the div? Can we specify just the nodes inside the div, of which we already have a passed id?
crosenblum
And then parent is the variable used to hold the id of the div we are looping through?
crosenblum
If you pass the div to the function i gave above, the function lets you do magic on all text nodes in the div.Pass the function your div element by first retrieving it in a variable `var theDiv = document.getElementById('theDivId');` and then supplying it to the function.
npup
I get error message saying parent is null.
crosenblum
Show more code and this won't be a problem :)
npup
My fault, I had the wrong div name. :)
crosenblum
Now I just need the value of the current text node inside the div we are at. Because I am doing language translation, 1 set of code for the whole page, 2nd set, for the ajax pages. And this is to help improve performance of the 2nd set.
crosenblum
This works, and does the job exactly as i need it. Been looking for an answer for weeks lol.Thank You very much!
crosenblum
If you mean the same thing as I do when i say "the value of a text node", you can reference it via the property `nodeValue` on a DOM node. "`child.nodeValue`" in the case of the loop in the function I gave above. Is that what you mean with "current" text node? The one currently looped over?
npup