Recursive function in javascript

Maybe is a trivial problem, i don't know why this function exit from for cycle when it goes on else statement. I need this function to fetch an xml document.

function xmlToArray(element){
    childs= element.childNodes;
    if(childs.length != 1){
      for(var i=0;i<childs.length;i++){
        if(childs[i].hasChildNodes()){
          xmlToArray(childs[i]);
        }
        alert("exit from if");
      }//end for
      alert("exit from for");
    }//end if
    else{
      alert("do something with element");
    }
    alert("end of func");
}

Answers


Since childs is not a local variable, all calls of xmlToArray work on the same data.

Try this:

function xmlToArray(element) {
    var childs = element.childNodes;
    // …
}

Using var declares that variable in the current scope.


Need Your Help

Return data from submitted form targeted to iframe

javascript jquery iframe file-upload web.py

I have a web.py application with which i have implemented a form that uploads a file to my server via post with the target set as an iframe in my page. This works a charm but I want to be able to s...

Generate .ics file on iOS

ios objective-c calendar eventkit icalendar

I'm building ios application that must generate .ics file from iPhone Calendar, but can't found solution for it. As I see EventKit Framework doesn't give this functionality, maybe some other

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.