473,837 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

accessing form data from javascript

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 called "form1", and I have selects called "PORTA", "PORTB" ...
etc...

I then had javascript that accessed these selects as below, and it worked
fine.

ind = document.form1. PORTD.selectedI ndex;
val = document.form1. PORTD.options[ind].value;
dev = document.form1. PORTD.options[ind].text;
My form is now autogenerated, and form data is stored to file, so I now use
an associative array for thte form elements (so that I can loop through them
easily), The form elements names are now :

McuCfg[PORTA], McuCfg{PORTB} and so on

Now, I modified the javascript so that it now uses the McuCfg[] associative
array :
ind = document.form1. McuCfg[PORTD].selectedIndex;
val = document.form1. McuCfg[PORTD].options[ind].value;
dev = document.form1. McuCfg[PORTD].options[ind].text;
When the script runs, I get the error

"document.form1 .McuCfg.PORTD is null or not an object"

I have used the same notation that i know works for the "options" array
although that's not an associative array.

Can anyone suggest how I might go about accessing the form elements form
javascripts.. google hasnt helped at all :-(

Thanks in advance

Chris


Jul 23 '05 #1
6 2749
Chris Styles wrote:
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 called "form1", and I have selects called "PORTA", "PORTB" ...
etc...

I then had javascript that accessed these selects as below, and it worked
fine.

ind = document.form1. PORTD.selectedI ndex;
val = document.form1. PORTD.options[ind].value;
dev = document.form1. PORTD.options[ind].text;
My form is now autogenerated, and form data is stored to file, so I now use
an associative array for thte form elements (so that I can loop through them
easily), The form elements names are now :

McuCfg[PORTA], McuCfg{PORTB} and so on

Now, I modified the javascript so that it now uses the McuCfg[] associative
array :
ind = document.form1. McuCfg[PORTD].selectedIndex;
val = document.form1. McuCfg[PORTD].options[ind].value;
dev = document.form1. McuCfg[PORTD].options[ind].text;
When the script runs, I get the error

"document.form1 .McuCfg.PORTD is null or not an object"
Correct.
I have used the same notation that i know works for the "options" array
although that's not an associative array.
Neither is the McuCfg array.
Can anyone suggest how I might go about accessing the form elements form
javascripts.. google hasnt helped at all :-(


Did Google turn up the FAQ for this group?

http://jibbering.com/faq/#FAQ4_25

That specific part of the FAQ deals with accessing form elements with []
in the name. It will also apply to elements with (), {} in the name as well.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #2
Chris Styles wrote:
<snip>
My form is now autogenerated, and form data is stored
to file, so I now use an associative array for thte
form elements (so that I can loop through them easily),
The form elements names are now :

McuCfg[PORTA], McuCfg{PORTB} and so on

Now, I modified the javascript so that it now uses the
McuCfg[] associative array :

ind = document.form1. McuCfg[PORTD].selectedIndex;
val = document.form1. McuCfg[PORTD].options[ind].value;
dev = document.form1. McuCfg[PORTD].options[ind].text;

When the script runs, I get the error

"document.form1 .McuCfg.PORTD is null or not an object"

I have used the same notation that i know works for the
"options" array although that's not an associative array.
The options property of SELECT elements is a collection, but there are
no associative arrays in javascript (Objects (the 'ECMAScript native
object') are as close as javascript gets). Your associative array is in
PHP (probably) and exists on the server, it has not existence on the
client, in javascript or the browser's DOM.
Can anyone suggest how I might go about accessing the
form elements form javascripts.. google hasnt helped
at all :-(


<URL: http://www.jibbering.com/faq/#FAQ4_25 >

Richard.
Jul 23 '05 #3

Where are you calling the function from, using what event, chances are,
you do not need to specify form names and such, as for using an
variable/element to address an obj, you bracket it:

myArray=['duck','wings'];
document.FORMNA MEHERE[myArray[0]].selectedIndex= 2;
------------ will select the 3rd option in
document.FORMNA MEHERE.duck select element ----
note that if you're event handler is from a form element, you can just
use this.form as argument for the function to address its siblings.

Danny

On Sun, 05 Jun 2005 16:18:33 -0700, Chris Styles
<ch***@nospam.d ustbubble.com> wrote:
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 called "form1", and I have selects called "PORTA", "PORTB"
...
etc...

I then had javascript that accessed these selects as below, and it worked
fine.

ind = document.form1. PORTD.selectedI ndex;
val = document.form1. PORTD.options[ind].value;
dev = document.form1. PORTD.options[ind].text;
My form is now autogenerated, and form data is stored to file, so I now
use
an associative array for thte form elements (so that I can loop through
them
easily), The form elements names are now :

McuCfg[PORTA], McuCfg{PORTB} and so on

Now, I modified the javascript so that it now uses the McuCfg[]
associative
array :
ind = document.form1. McuCfg[PORTD].selectedIndex;
val = document.form1. McuCfg[PORTD].options[ind].value;
dev = document.form1. McuCfg[PORTD].options[ind].text;
When the script runs, I get the error

"document.form1 .McuCfg.PORTD is null or not an object"

I have used the same notation that i know works for the "options" array
although that's not an associative array.

Can anyone suggest how I might go about accessing the form elements form
javascripts.. google hasnt helped at all :-(

Thanks in advance

Chris




--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #4
Chris Styles wrote:
[...]
My form is now autogenerated, and form data is stored to file, so I now use
an associative array for thte form elements (so that I can loop through them
easily), The form elements names are now :

McuCfg[PORTA], McuCfg{PORTB} and so on

Now, I modified the javascript so that it now uses the McuCfg[] associative
array :
ind = document.form1. McuCfg[PORTD].selectedIndex;
val = document.form1. McuCfg[PORTD].options[ind].value;
dev = document.form1. McuCfg[PORTD].options[ind].text;
When the script runs, I get the error

"document.form1 .McuCfg.PORTD is null or not an object"
To answer your specific question, the above line tells you how the
browser is interpreting your statement.

When accessing form elements using dot notation like this, the format
is:

document.<formN ame>.<elementNa me>.<...>;

What comes after the elementName could be another element name (for
those elements that have children, such as a select) or a property of
the element (name, id, etc.).

In your code, 'McuCfg' is being read as an element name, the content of
the [] is being read as either an index to the collection of child
elements or the name of a child element. So the line references a form
named 'form1' with a child called 'McuCfg'. 'PORTD' is being passed as
a variable - it should contain either an index or string that is the
name of a child of 'McuCfg'. But since there is no element 'McuCfg',
the script doesn't get that far. To use 'PORTD' as a literal string,
it needs to be quoted (but that wont help here anyway...).

Your second issue is with your array notation. If your array is:

var McuCfg = [ 'PORTA','PORTB' ,'PORTC','PORTD ' ]

Then a reference to 'PORTD' will be 'McuCfg[3]', there is no element
'McuCfg[PORTD]'.

A more general way of accessing forms and their elements is using the
forms and elements collections:

document.forms[formName].elements[elementName]...

or

document.forms[index].elements[index]...

You can mix the notations together, though I'm not sure whether that is
viewed as good practice for maintenance reasons.

To fix your code, use the elements collection:

ind = document.form1. elements[McuCfg[3]].selectedIndex;

I have used the same notation that i know works for the "options" array
although that's not an associative array.
It's an HTML collection, which is like an array only different.

Can anyone suggest how I might go about accessing the form elements form
javascripts.. google hasnt helped at all :-(


Here's some play code with a couple of different ways of accessing form
elements:
<form name="form1" action="">
<input name="PORTA" type="text" value="PORTA value"><br>
<input name="PORTB" type="text" value="PORTB value"><br>
<input name="PORTC" type="text" value="PORTC value"><br>
<input name="PORTD" type="text" value="PORTD value"><br>
<input type="button" value="Do stuff" onclick="
doStuff();
">
</form>
<script type="text/javascript">

function doStuff(){
var McuCfg = [ 'PORTA','PORTB' ,'PORTC','PORTD ' ]

alert( 'Direct access: '
+ document.forms['form1'].elements[McuCfg[3]].value);

for ( var i=0, j=McuCfg.length ; i<j; i++){
alert('Looping: '
+ document.form1. elements[McuCfg[i]].value );
}
}
</script>

--
Rob
Jul 23 '05 #5
Rob,

Thanks for such a concise reply. As you may all have guessed, I'm rather new
to Javascript, and so some of the concepts are still a little fuzzy. This
one is crystal clear now.. :-)

Cheers

Chris
"RobG" <rg***@iinet.ne t.auau> wrote in message
news:4X******** *********@news. optus.net.au...
Chris Styles wrote:
[...]
My form is now autogenerated, and form data is stored to file, so I now use an associative array for thte form elements (so that I can loop through them easily), The form elements names are now :

McuCfg[PORTA], McuCfg{PORTB} and so on

Now, I modified the javascript so that it now uses the McuCfg[] associative array :
ind = document.form1. McuCfg[PORTD].selectedIndex;
val = document.form1. McuCfg[PORTD].options[ind].value;
dev = document.form1. McuCfg[PORTD].options[ind].text;
When the script runs, I get the error

"document.form1 .McuCfg.PORTD is null or not an object"


To answer your specific question, the above line tells you how the
browser is interpreting your statement.

When accessing form elements using dot notation like this, the format
is:

document.<formN ame>.<elementNa me>.<...>;

What comes after the elementName could be another element name (for
those elements that have children, such as a select) or a property of
the element (name, id, etc.).

In your code, 'McuCfg' is being read as an element name, the content of
the [] is being read as either an index to the collection of child
elements or the name of a child element. So the line references a form
named 'form1' with a child called 'McuCfg'. 'PORTD' is being passed as
a variable - it should contain either an index or string that is the
name of a child of 'McuCfg'. But since there is no element 'McuCfg',
the script doesn't get that far. To use 'PORTD' as a literal string,
it needs to be quoted (but that wont help here anyway...).

Your second issue is with your array notation. If your array is:

var McuCfg = [ 'PORTA','PORTB' ,'PORTC','PORTD ' ]

Then a reference to 'PORTD' will be 'McuCfg[3]', there is no element
'McuCfg[PORTD]'.

A more general way of accessing forms and their elements is using the
forms and elements collections:

document.forms[formName].elements[elementName]...

or

document.forms[index].elements[index]...

You can mix the notations together, though I'm not sure whether that is
viewed as good practice for maintenance reasons.

To fix your code, use the elements collection:

ind = document.form1. elements[McuCfg[3]].selectedIndex;

I have used the same notation that i know works for the "options" array
although that's not an associative array.


It's an HTML collection, which is like an array only different.

Can anyone suggest how I might go about accessing the form elements form
javascripts.. google hasnt helped at all :-(


Here's some play code with a couple of different ways of accessing form
elements:
<form name="form1" action="">
<input name="PORTA" type="text" value="PORTA value"><br>
<input name="PORTB" type="text" value="PORTB value"><br>
<input name="PORTC" type="text" value="PORTC value"><br>
<input name="PORTD" type="text" value="PORTD value"><br>
<input type="button" value="Do stuff" onclick="
doStuff();
">
</form>
<script type="text/javascript">

function doStuff(){
var McuCfg = [ 'PORTA','PORTB' ,'PORTC','PORTD ' ]

alert( 'Direct access: '
+ document.forms['form1'].elements[McuCfg[3]].value);

for ( var i=0, j=McuCfg.length ; i<j; i++){
alert('Looping: '
+ document.form1. elements[McuCfg[i]].value );
}
}
</script>

--
Rob

Jul 23 '05 #6
Chris Styles wrote:
Rob,

Thanks for such a concise reply. As you may all have guessed, I'm rather new
to Javascript, and so some of the concepts are still a little fuzzy. This
one is crystal clear now.. :-)


Glad to help, but please follow the convention and do not top post.
Quote only what is relevant and place your reply below the qouted text.

Have a read of the group FAQ:

<URL:http://www.jibbering.c om/faq>
--
Rob
Jul 23 '05 #7

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

Similar topics

2
2836
by: Tim Blackwell | last post by:
i have some php code pulling some data from a database and i need to access some of hte variables in java script. i've never done this before. how can i do it. eg. i'm loading a combo box from the database (this works fine). but, i'd like to send some description data to a text area when user selects something, from the combo using, JS. when they click a button they would go to a website (this works fine for me as well) any help...
1
1532
by: Esben Rune Hansen | last post by:
Hi I am working on a PHP-script and need javascript to set the value of a hidden field in a form. This field happens to be an entry in an array data according to my example. How can I do this? Below is listet two PHP-pages: one that doesn't work (to my dismay), and another that does work, but do not use an array entry in the hidden field. Thanks
18
2112
by: Choxio | last post by:
Using ASP you can access posted form elements. Is it possible to access them from JavaScript? myHTML.htm <form action=myASP.asp> <input type=hidden id=f1 value=val1> <input type=hidden id=f2 value=val2> <input type=submit value="Choose Me"> </form>
5
1989
by: Peter | last post by:
I have 3 ASP.NET forms written in C#, I also have 3 frames (left, top and bottom) one for each form. My question is if I have data on one form in the bottom frame how do I access this data when I click on the button which is on the top frame? I want to save the data from the bottom frame to the database when a user clicks on the Update button. Does anyone have an example on how to do this? ...
1
3142
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
0
1467
by: YAH | last post by:
I don't know where to post this so I'll try here. I'm trying to create a C# based Windows Form app which hosts a webbrowser control, loads a webpage of mine, and after DocumentComplete extracts data that is stored in javascript arrays. FWIW, the javascript arrays aren't declared in the head or body of the webpage; they are globals in an included .js file. Could someone provide a brief explanation or example snippet that would guide me...
0
12092
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the likelihood of CRM success from less than 20 percent to 60 percent. WHITEPAPER :
7
3830
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a database search that I am unable to figure out how to access. (The search is implemented in Php/MySQL.) The user enters search values for: name, address1, city, .... etc., ..... and for each of these they also select whether the search should...
3
10526
by: judy.j.miller | last post by:
Does anyone know why i can't access a form element value using dot notation in firefox, when i'm in a function. Works ok in the body. I'm trying to do this: var FarTemp = faren.temp.value; I can get at the value using the array method, the getelements by id method, and the bracket-with-the-element-name in it method. But the dot notation doesn't work, in firefox, in the function (which i have in the head).
0
9682
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10881
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10575
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10275
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7807
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7004
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5850
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4475
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 we have to send another system
3
3126
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.