Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive Function not working 1

Status
Not open for further replies.

solepixel

Programmer
May 30, 2007
111
US
I've created a recursive function to find the form in which an object sits inside of. my function is:
Code:
function getParentForm(el){
    var parent_ref;
    if(el.parentNode.tagName != "FORM"){
        getParentForm(el.parentNode);
    } else {
        if(el.parentNode.name){
            parent_ref = el.parentNode.name;
        } else {
            parent_ref = el.parentNode.id;
        }
    }
    alert(parent_ref); // this has the correct value in it
    return parent_ref;
}

function otherFunction(elm){
    parentForm = getParentForm(elm);
    alert(parentForm); // does not alert the same as the alert above.
}

why will it alert the correct value in the first function, but no the second function?
 
Hi

Why all that ? The [tt]form[/tt] elements have a [tt]form[/tt] property pointing to their [tt]form[/tt].

Like in the below example :
Code:
<html>
<head>
<style type="text/css">
input { width: 100%; }
</style>
<script type="text/javascript">
function me(who)
{
  who.value='I am in form "'+who[red].form[/red].name+'" and will be submitted to "'+who[red].form[/red].action+'"'
}
</script>
</head>
<body>
<form action="/first.htm" name="one" onsubmit="return false">
<input type="text" onfocus="me(this)">
</form>
<form action="/second.htm" name="two" onsubmit="return false">
<input type="text" onfocus="me(this)">
</form>
</body>
</html>

Feherke.
 
It worked for me when I used it in this example:
Code:
<html>
  <head>
 <script>
function getParentForm(el){
    var parent_ref;
    if(el.parentNode.tagName != "FORM"){
        getParentForm(el.parentNode);
    } else {
        if(el.parentNode.name){
            parent_ref = el.parentNode.name;
        } else {
            parent_ref = el.parentNode.id;
        }
    }
    alert(parent_ref); 
    return parent_ref;
}

function otherFunction(elm){
    parentForm = getParentForm(elm);
    alert(parentForm); // does not alert the same as the alert above.
}
</script>
</head>
<body>
<form name="xx">
<a href="" onclick="otherFunction(this);return false">in form xx</a>
</form>
<form name="yy">
<a href="" onclick="otherFunction(this);return false">in form yy</a>
</form>
</body>
</html>

However, I would avoid recursion when it is not needed. Recursive functions can potentially eat a lot of memory on the client's box. Here is an example that does not use recursion:
Code:
<html>
  <head>
 <script>
function getParentForm(el){
  while(el.parentNode != null && el.tagName != "FORM") el = el.parentNode;
  return (el.name) ? el.name : el.id;
}

function otherFunction(elm){
    parentForm = getParentForm(elm);
    alert(parentForm); 
}
</script>
</head>
<body>
<form name="xx">
<a href="" onclick="otherFunction(this);return false">in form xx</a>
</form>
<form name="yy">
<a href="" onclick="otherFunction(this);return false">in form yy</a>
</form>
</body>
</html>

Adam
 
Excellent Adam, that's exactly what I was trying to do. and it avoided the recursion! Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top