Finding an <input>'s <form>-parent via DOM?

Hi,

I'm just wondering..
How would you walk the DOM-tree in order to find the parent <FORM> element (eg. to submit() it) of any given <INPUT> element?

Example:
<form action="sth"> <!-- no name or id or class! -->
<input type="text" onClick="alert (find_parental_form_element(this))">
<input type="text" onClick="alert (find_parental_form_element(this))">
<input type="text" onClick="alert (find_parental_form_element(this))">
</form>

Walking via .parentNode only returns me styling - type elements (eg. in this order: DIV, TD, TR, TBODY, TABLE).

Ys
LVL 4
ysreAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ysreAuthor Commented:
Never mind, found a (kinda quirky) solution already.

I'm walking back via .parentNode until I find a node that has a childnode which has a nodeName of "FORM" ;)

Ys
clockwatcherCommented:
Wouldn't that have problems if you had this?

<p>

  <form>
   <input>
  </form>

  <form>
    <input>
   </form>

</p>


Rather than backtracking you could explicitly search for the element.

<html>
      <script language="javascript">
            function find_parental_form_element(el)
            {
                  for (var i=0; i < document.forms.length; i++)
                  {
                        var d = document.forms[i];
                        
                        for (var x=0; x < d.elements.length; x++)
                        {
                              if (d.elements[x] == el)
                              {
                                    // return d;
                                    return d.action;  // just to show we have the right form
                              }
                        }
                  }      
            }
            
      </script>
      
      <body>
      <form></form>
<form action="sth1"> <!-- no name or id or class! -->
<input type="text" onClick="alert (find_parental_form_element(this))">
<input type="text" onClick="alert (find_parental_form_element(this))">
<input type="text" onClick="alert (find_parental_form_element(this))">
</form>
<form action="sth2"> <!-- no name or id or class! -->
<input type="text" onClick="alert (find_parental_form_element(this))">
<input type="text" onClick="alert (find_parental_form_element(this))">
<input type="text" onClick="alert (find_parental_form_element(this))">
</form><form action="sth3"> <!-- no name or id or class! -->
<input type="text" onClick="alert (find_parental_form_element(this))">
<input type="text" onClick="alert (find_parental_form_element(this))">
<input type="text" onClick="alert (find_parental_form_element(this))">
</form>      
      </form>
      </body>
</html>
ysreAuthor Commented:
Hm yeah, I added some prerequisites (that's why I called it quirky).
Just wanted to find out if you can do it via the appraised ;) DOM 1 model.
Apparently you can't really.

Unless you use the old-style document.forms approach (or getElementsByTagName for firefox).

Thanks for the insight :)

smaccariCommented:
All inputs have a property named form that reference the form containing it.

<form>
<input type=text name=foo id=foo>
</form>

<script>
var myForm=document.getElementById("foo").form; //this will return the form element
</script>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ysreAuthor Commented:
Hmpf sometimes things can be so easy..
Funny it never showed up in the properties' listing for the element(s).

Thanks :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.