473,404 Members | 2,170 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,404 software developers and data experts.

Specifying a form by name? Problems with document.forms[x]

Hello,

I have code similar to:

if (beforeToday(document.forms[0].sStartDate.value))

that was broken when an include file had a form added to it. Since my
form was no longer the 0th form, my code was broken. Is there any way
to specify a form by name or some other work around for this
situation?

-Eric

Mar 7 '07 #1
6 18727
"Eric" <er**********@gmail.comwrote in message
news:11**********************@8g2000cwh.googlegrou ps.com...
Hello,

I have code similar to:

if (beforeToday(document.forms[0].sStartDate.value))

that was broken when an include file had a form added to it. Since my
form was no longer the 0th form, my code was broken. Is there any way
to specify a form by name or some other work around for this
situation?
Sure:

<form name="form1">

if (beforeToday(document.form1.sStartDate.value))
Mar 7 '07 #2
On Mar 7, 12:16 am, "Eric" <eric.gofo...@gmail.comwrote:
Hello,

I have code similar to:

if (beforeToday(document.forms[0].sStartDate.value))

Is there any way
to specify a form by name or some other work around for this
situation?
Use:
document.forms["form_name"]

Example:
----------
....
<script ... >
function test(){
alert(document.forms["form1"].sStartDate.value)
}

</script>
....
<form name="form1" onsubmit="test();return false" action="#" ...>
<input name="sStartDate" type="text">
<input type="submit" value="Test">
....

</form>
....
---------

that means -
document.forms[formIndex] == document.forms[formNameAsString]

Mar 7 '07 #3
On Mar 7, 11:57 am, scripts.cont...@gmail.com wrote:
On Mar 7, 12:16 am, "Eric" <eric.gofo...@gmail.comwrote:
Hello,
I have code similar to:
if (beforeToday(document.forms[0].sStartDate.value))
Is there any way
to specify a form by name or some other work around for this
situation?

Use:
document.forms["form_name"]

Example:
----------
...
<script ... >
function test(){
alert(document.forms["form1"].sStartDate.value)

}

</script>
...
<form name="form1" onsubmit="test();return false" action="#" ...>
<input name="sStartDate" type="text">
<input type="submit" value="Test">
...

</form>
...
---------

that means -
document.forms[formIndex] == document.forms[formNameAsString]
you can use document.getElementById('id') to get any element.
Element used by this method won't uses forms so no problem in
accessing the elements.

Mar 7 '07 #4
In comp.lang.javascript message <ZN6dnbNg08nKwXPYnZ2dnUVZ_oWdnZ2d@comcas
t.com>, Wed, 7 Mar 2007 00:31:30, McKirahan <Ne**@McKirahan.composted:
>"Eric" <er**********@gmail.comwrote in message
news:11**********************@8g2000cwh.googlegro ups.com...
>Hello,

I have code similar to:

if (beforeToday(document.forms[0].sStartDate.value))

that was broken when an include file had a form added to it. Since my
form was no longer the 0th form, my code was broken. Is there any way
to specify a form by name or some other work around for this
situation?

Sure:

<form name="form1">

if (beforeToday(document.form1.sStartDate.value))
You are a reliable source of bad advice, here as elsewhere. Code which
works in some systems but not in others, or which is unnecessarily not
standards-compliant, is not good code.

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 8 '07 #5
jontyus said the following on 3/7/2007 4:31 AM:
On Mar 7, 11:57 am, scripts.cont...@gmail.com wrote:
>On Mar 7, 12:16 am, "Eric" <eric.gofo...@gmail.comwrote:
>>Hello,
I have code similar to:
if (beforeToday(document.forms[0].sStartDate.value))
Is there any way
to specify a form by name or some other work around for this
situation?
Use:
document.forms["form_name"]
<snip>
you can use document.getElementById('id') to get any element.
If and only if that element has a unique ID attribute, or, you are using
a broken browser (namely, IE). Otherwise, just use the forms collection.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 10 '07 #6
Dr J R Stockton said the following on 3/8/2007 1:45 PM:
In comp.lang.javascript message <ZN6dnbNg08nKwXPYnZ2dnUVZ_oWdnZ2d@comcas
t.com>, Wed, 7 Mar 2007 00:31:30, McKirahan <Ne**@McKirahan.composted:
>"Eric" <er**********@gmail.comwrote in message
news:11**********************@8g2000cwh.googlegro ups.com...
>>Hello,

I have code similar to:

if (beforeToday(document.forms[0].sStartDate.value))

that was broken when an include file had a form added to it. Since my
form was no longer the 0th form, my code was broken. Is there any way
to specify a form by name or some other work around for this
situation?
Sure:

<form name="form1">

if (beforeToday(document.form1.sStartDate.value))

You are a reliable source of bad advice, here as elsewhere. Code which
works in some systems but not in others, or which is unnecessarily not
standards-compliant, is not good code.
Can you name a "system" (OS/browser) where document.formName won't get a
reference to the form as long as the form name doesn't contain illegal
characters? Or, is your anti-McK attitude shining through again?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 10 '07 #7

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

Similar topics

4
by: Eric | last post by:
Hey Everyone.. I have a form that has approximately 7 text fields and 1 checkbox. Generally when this form is submitted(to itself BTW) it works fine, however, when the checkbox is only field...
4
by: Jan-Friedrich Mutter | last post by:
Hi, I have a problem to access a hidden field by javascript. The name of the field has a dot in it. That's the problem. But I need that dot because it is a "Stuts Property". I'm using IE 6....
12
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" ...
2
by: C Gillespie | last post by:
Dear All, I have 2 arrays var A1 = new Array(); A1 ="Y2"; var B1 = new Array(); B1 ="Y1"; B1 ="sink";
3
by: frey | last post by:
i have two parts of code here this is a javascript: function addThirdTierCategory(){ var length = document.form.selectedCategory.options.length var maxNum=10; if(length >= maxNum){...
3
by: MeNotHome | last post by:
I am trying to automate web browser navigation and form fill out in vb.net Why doesn't this work? AxWebBrowser1.Document.Forms(0).All("action-download").click() I also tried...
1
by: John Smith | last post by:
If I have reference to some TextBox, how can I find its form name (eg. "document.Form1")?
2
by: vajra1987 | last post by:
Hi guys I have the following situation: In mypage1.jsp I have 4 forms, each with a different name (and id). But on these forms, there are some fields that have the same name, for example ...
23
by: Stanimir Stamenkov | last post by:
<form name="myForm" action="..."> <p><input type="text" name="myElem"></p> </form> As far as I was able to get the following is the standard way of accessing HTML form elements: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.