www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] get text inside tags, strange use of variable name

  1. #1
    Join Date
    Dec 2010
    Location
    Brazil
    Posts
    32

    resolved [RESOLVED] get text inside tags, strange use of variable name

    I have a function to get text inside tags. One version works the other does not. The only difference is that in one I use 'elem' all the time, and in the other version I assign 'elem' to 'elems'. I'm really dumbfounded.

    Code:
    /* This version does NOT work becuase I am using 'elems' (note the s). */
    function getText1(elem) {
        var str = '';
    
        elems = elem.childNodes || elem;
        for (var i = 0; i < elems['length']; i++) {
            //str += elem[i].nodeType != 1 ? elem[i].nodeValue : getText(elem[i].childNodes);
            if (elems[i].nodeType != 1)
                str += elems[i].nodeValue;
            else
                str += getText(elems[i].childNodes);
        }
        return str;
    }
    Code:
    /* This one works. Not using 'elems'. Using 'elem' all the time. */
    function getText2(elem) {
        var str = '';
    
        elem = elem.childNodes || elem;
        for (var i = 0; i < elem['length']; i++) {
            //str += elem[i].nodeType != 1 ? elem[i].nodeValue : getText(elem[i].childNodes);
            if (elem[i].nodeType != 1)
                str += elem[i].nodeValue;
            else
                str += getText(elem[i].childNodes);
        }
        return str;
    }

    Sample HTML:
    Code:
    <p>Hello, <strong>how</strong> are you doning?</p>
    Results
    Code:
    var v = document.getElementsByTagName('p')[0];
    console.log(getText1(v)); // prints 'Hello, how'
    console.log(getText2(v)); // prints 'Hello, how are you doing?'.
    What is going on? Why getText1() partially works, while getText2() works as expected?

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    It has nothing to do with the variable name. getText1 doesn't work because elems is a global variable (thus the recursive calls to the function share the same variable). Putting var in front of elems will make it work just like the second function.
    New to web development or in need of a good reference? Check out the Mozilla Developer Network or W3Schools.

  3. #3
    Join Date
    Dec 2010
    Location
    Brazil
    Posts
    32
    Quote Originally Posted by ReFreezed View Post
    It has nothing to do with the variable name. getText1 doesn't work because elems is a global variable (thus the recursive calls to the function share the same variable). Putting var in front of elems will make it work just like the second function.
    Of course. Since getText2(elem) uses 'elem' as argument, it is automatically a local variable. Thanks.

    By the way, I remember when I was studying C, I also had a problem once, and it was the same problem of variable scope. You calling attention to it reminded me of that. I'm still not completely aware of how recursion works. I know you are calling the same function over and over, but it is no clear to me what happens internally.

    EDIT: I marked the thread as resolved, but if someone has anything more to say about the subject, please, fell free to do so.
    Last edited by FernandoBasso; 01-21-2013 at 09:26 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles