473,512 Members | 14,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1976
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
12877
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
1373
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
7144
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
2850
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
2716
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
1879
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
2203
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
1404
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
333
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
4000
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
7254
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
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
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,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.