473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Determine FORM that contains a specific INPUT element?

I created this test routine to return the form containing a certain
input element:

function GetElementForm(element)
{
// Return the form that contains element.

var myElement = element;

while (myElement)
{
myElement = myElement.parentNode;
if (myElement)
{
var sTagName = myElement.tagName;
if (sTagName)
{
if (sTagName.toLowerCase() == "form")
{
break;
}
}
}
}
return myElement;
}

This seems to work for "well formed" HTML, but fails for example, when
a form is defined within a table.

Is there a technique that works reliably?

Thanks,

Frank

Oct 11 '06 #1
2 1972
Frank wrote:
... return the form containing a certain input element:
[snip]
Is there a technique that works reliably?
formElement.form

The property, form, is defined for all form controls (button, fieldset,
input, label, legend, object, option, select, textarea). If the controls
is a descendant of a form, the property will refer to that object - it
is null, otherwise.

Mike
Oct 11 '06 #2
Frank wrote:
<snip>
This seems to work for "well formed" HTML, but
fails for example, when a form is defined within
a table.
That is not true. A form can be defined within a table (specifically,
contained within a single TD or TH element) and the chain of parent nodes
of its form controls will include its form ancestor.

Generally, if you want to use a table to 'lay-out' form controls (a
reasonable thing to do in semantic HTML as forms can be tabular) you
would wrap the Form elements around the TABLE, and so (potentially) have
structurally valid HTML to start with.
Is there a technique that works reliably?
You could try following the general advice that if a document is to be
scripted it should always be a structurally valid document to start with.
Doing that completely avoids all the consequences of inconsistent and
unexpected DOM structures that result form browser error correction in
tag-soup documents. (That is; don't make things difficult for yourself
when a simple (potentially mechanised) quality check on the HTML can
eliminate many issues at a stroke).

Richard.
Oct 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: JT | last post by:
is there a way to determine if a form object actually exists? if i use the following syntax: varTextField = Request.Form("txtFormField") then varTextField = "" which gives the same result...
4
by: Paweł | last post by:
We can access the Nth element in the first form by document.forms.elements imagine that theres function like <INPUT TYPE=TEXT" onclick="somefunction(this)"> and... we have function...
8
by: L Major | last post by:
Hi Unfortunately, I am limited to using tables for part of my current project. I have a form that spans across a number of TR and TD in the shape of checkboxes. Doctype is XHTML 1.0...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
5
by: ojvm | last post by:
ok. thanks again for the time spend reading this. this code adds 2 controls in html form but it places in top of the form. i want this control1 control2 control1 control2 control1 ...
3
by: Jordan | last post by:
I am dynamically inserting an html <input> tag as text (equivalent of an image button) into a page via a Literal control. Something like this gets inserted: <input type="image"...
3
by: Bill_W_Stephens | last post by:
I am modifying existing javascript code where the author appears to be passing an entire form to a javascript function called Require(). I want to add a new test: "is @BHFOR.value = blanks". ...
1
by: Frank | last post by:
I created this test routine to return the form containing a certain input element: function GetElementForm(element) { // Return the form that contains element. var myElement = element; ...
5
by: mantrid | last post by:
Hello Im trying to find the correct syntax to use to determine whether which form element is visible on changing the value in a dropdown list I need something in the onChange of the dropdown...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.