Finding the FORM that an element belongs to in JavaScript
Posted
by Magnus Smith
on Stack Overflow
See other posts from Stack Overflow
or by Magnus Smith
Published on 2009-01-21T13:37:59Z
Indexed on
2010/04/01
16:23 UTC
Read the original article
Hit count: 420
How can I find out which FORM an HTML element is contained within, using a simple/small bit of JavaScript? In the example below, if I have already got hold of the SPAN called 'message', how can I easily get to the FORM element?
<form name="whatever">
<div>
<span id="message"></span>
</div>
</form>
The SPAN might be nested within other tables or DIVs, but it seems too long-winded to iterate around .parentElement and work my way up the tree. Is there a simpler and shorter way?
If it wasn't a SPAN, but an INPUT element, would that be easier? Do they have a property which points back to the containing FORM? Google says no...
© Stack Overflow or respective owner