473,386 Members | 1,815 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,386 software developers and data experts.

Addressing form elements using array

Hi,

im using Struts with nested tags for form validation.
So I end up with form elements named like this

risikenManuell[0].praemieNeu
risikenManuell[1].praemieNeu
....

I would like to do a calculation on the input values
using javascript but I don't know how to
address the fields.

With alert(document.vertragEditForm.risikenManuell[0].praemieNeu);
I get the error, that this element is null or not an object.

Is there a way to do it.

TIA
Frank
Jul 20 '05 #1
12 3601
Lee
Frank Stephan said:
With alert(document.vertragEditForm.risikenManuell[0].praemieNeu);
I get the error, that this element is null or not an object.

Is there a way to do it.


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

Jul 20 '05 #2
fr***********@gmx.de (Frank Stephan) writes:
So I end up with form elements named like this

risikenManuell[0].praemieNeu .... With alert(document.vertragEditForm.risikenManuell[0].praemieNeu);
I get the error, that this element is null or not an object.

Is there a way to do it.


Yes, and it's even in the FAQ:
<URL:http://jibbering.com/faq/#FAQ4_25>
In your case:
document.forms['vertragEditForm'].elements['riskenManuell[0].praemieNeu']

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
On 7 Jan 2004 09:05:25 -0800, Frank Stephan <fr***********@gmx.de> wrote:
Hi,

im using Struts with nested tags for form validation.
So I end up with form elements named like this

risikenManuell[0].praemieNeu
risikenManuell[1].praemieNeu
...
You mean like: <INPUT ... name="risikenManuell[0]">
I would like to do a calculation on the input values
using javascript but I don't know how to
address the fields.

With alert(document.vertragEditForm.risikenManuell[0].praemieNeu);
An style comment: you should use fully-qualified references to help
prevent name collisions. That is, window.alert(...), not alert(...).
I get the error, that this element is null or not an object.


The problem with your line above is that brackets ([ ]) are operators,
used to subscript arrays. The JavaScript engine will interpret the
risikenManuell[0] above as 'evaluate the first element of the array,
risikenManuell'. To get around this, and other cross-browser problems, use
the collections syntax:

document.forms['form_name'].elements['control_name'].method_or_property

For example,

document.forms['vertragEditForm'].elements['risikenManuell[0]'].praemieNeu

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #4
In article <op**************@news-text.blueyonder.co.uk>, Michael Winter
<M.******@blueyonder.co.invalid> writes:
With alert(document.vertragEditForm.risikenManuell[0].praemieNeu);


An style comment: you should use fully-qualified references to help
prevent name collisions. That is, window.alert(...), not alert(...).


Is there a browser that will alert when using window.alert(...) but *not alert*
when using alert(...) ??

If there isn't, then it seems superfluous.
--
Randy
Jul 20 '05 #5
hi************@aol.com (HikksNotAtHome) writes:
Is there a browser that will alert when using window.alert(...) but
*not alert* when using alert(...) ??


Only if you have created another variable in scope with the name
"alert". However, symmetrically, "window.alert" will fail if you have
another variable called "window" in scope.

I don't see any argument for writing "window." in front of global
variables. I do it in *one* case: in front of "window.open" mainly for
readability. It clearly distinguishes it from "document.open".

Otherwise I just write "alert", just as I write "parseFloat", "Object"
and "Array" ... and "window". There is no difference between these,
they are all just global variables (i.e., properties of the global
object).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
In article <ek**********@hotpop.com>, Lasse Reichstein Nielsen <lr*@hotpop.com>
writes:
hi************@aol.com (HikksNotAtHome) writes:
Is there a browser that will alert when using window.alert(...) but
*not alert* when using alert(...) ??


Only if you have created another variable in scope with the name
"alert". However, symmetrically, "window.alert" will fail if you have
another variable called "window" in scope.


I already knew that, I wanted to see if he knew it. And my reply seemed more
polite than a "Thats utterly false"
--
Randy
Jul 20 '05 #7
On 07 Jan 2004 23:39:56 GMT, HikksNotAtHome <hi************@aol.com> wrote:
In article <op**************@news-text.blueyonder.co.uk>, Michael Winter
<M.******@blueyonder.co.invalid> writes:
An style comment: you should use fully-qualified references to help
prevent name collisions. That is, window.alert(...), not alert(...).


Is there a browser that will alert when using window.alert(...) but *not
alert*
when using alert(...) ??

If there isn't, then it seems superfluous.


Read what I said again: "...to help prevent name collisions." I said
nothing about browsers not accepting alert(...). Furthermore, I said that
it was a style comment - therefore is was subjective, optional, and had no
direct relationship to the solution.

I take Mr Nielsen's point about the possibility of failure when a
variable, window, is present in the same scope. However, I would think
that no-one would create such a variable when accessing a window property
or method. Especially when there would be no need to call it 'window', and
a better, more descriptive name probably exists* (newWindow, popupWindow,
helpWindow, etc.)

Mike
* The first two examples would only be 'better' names if used soon after
the creation of a window and only for a short time.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #8
Hi,

Michael Winter wrote:
Read what I said again: "...to help prevent name collisions." I said
nothing about browsers not accepting alert(...). Furthermore, I said
that it was a style comment - therefore is was subjective, optional, and
had no direct relationship to the solution.

I take Mr Nielsen's point about the possibility of failure when a
variable, window, is present in the same scope. However, I would think
that no-one would create such a variable when accessing a window
property or method. Especially when there would be no need to call it
'window', and a better, more descriptive name probably exists*
(newWindow, popupWindow, helpWindow, etc.)

Mike
* The first two examples would only be 'better' names if used soon after
the creation of a window and only for a short time.


Which brings us to the next question: Who would be silly enough to use
"alert" as a global variable, resp. function name? The risk of name
collision exists in both cases. Concretely, there is nothing which
speaks in favour of using window.alert() instead of alert(), and there
is at least one argument against it: More code is sent to the client
which increases the file size ;-)

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #9
On Thu, 08 Jan 2004 08:39:07 +0100, Laurent Bugnion, GalaSoft
<galasoft-LB@bluewin_NO_SPAM.ch> wrote:
Which brings us to the next question: Who would be silly enough to use
"alert" as a global variable, resp. function name? The risk of name
collision exists in both cases. Concretely, there is nothing which
speaks in favour of using window.alert() instead of alert(), and there
is at least one argument against it: More code is sent to the client
which increases the file size ;-)


I would argue in favour of consistency. One would qualify references to
methods and properties, such as open() and close(), to distinguish between
the window and document members. Why not do it all the time? Why make
special cases?

Anyway, I already said that this was a matter of personal preference, so
let's not get hung-up on it, shall we?

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #10
"Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_SPAM.ch> wrote in
message news:bt**********@rex.ip-plus.net...
<snip>
... and there is at least one argument against it:
More code is sent to the client which increases the
file size ;-)


To which it could be added that the qualified reference will take
fractionally longer to resolve.

There is a context in which it probably is advisable to (at leas
sometimes) provide more qualified references, and it is because of the
scope considerations that Lasse mentioned. The strings provided as
values of event handling attributes are used as the function bodies of
internally generated function objects assigned as methods of the
corresponding HTML elements. And some browsers provide those functions
with custom scope handling features that effectively add various objects
from the DOM to the scope chain of the function. One object that is
commonly added to such a scope chain is the - document - so a call to -
open() - may be resolved as - document.open - or as - window.open -
depending on whether the browser places the - document - in the scope
chain or not.

Another context where qualifying a global property with - window - (or
some similar property that refers to the global object) is when
performing type-converting test on global properties:-

if(location){
...
}

would usually produce an error if - location - was undefined, while:-

if(window.location){
...
}

- will happily type-convert to true/false regardless. Though:-

if(typeof location != 'undefined'){
...
}

- does not produce an error even if - location - is undefined (but it is
a slower operation).

I don't think that I would ever bother qualifying - alert("xxx"); - (but
alert is more of a debugging tool anyway so there is maybe no need to
worry about the validity of the assumption that it exists in the
environment).

Richard.
Jul 20 '05 #11
Michael Winter <M.******@blueyonder.co.invalid> writes:

[variable called "window"]
However, I would think that no-one would create such a variable when
accessing a window property or method.


While I would probably not make one, thinking about it I believe that
I would be more likely to make a variable called "window" than one
called "alert". I often need to name the return value of window.open,
and while "newWindow" is better, it's also longer. But that's probably
why it's usually just "w".
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #12
Michael Winter <M.******@blueyonder.co.invalid> writes:
I would argue in favour of consistency. One would qualify references
to methods and properties, such as open() and close(), to distinguish
between the window and document members. Why not do it all the time?
Why make special cases?
Are you planning on writing:
window.eval, window.parseFloat, new window.Object, new window.Function,
window.document.body, and window.document.getElementById? Or event
window.window :)

And why "window", why not "self", which is a reference to the same object?
Anyway, I already said that this was a matter of personal preference,
so let's not get hung-up on it, shall we?


Arh, you no phun! :P
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #13

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

Similar topics

3
by: CES | last post by:
All, Is their a way of iterating thru each tag within a form and returning the value given in the id property, by that I mean the below html would return the values idBoxOne, idBoxTwo,...
2
by: Edward | last post by:
The below code builds 2 tables 4 rows by 4 cols. All cells have checkboxes. When checked, the checkboxes in the first column automatically check the remainder of the check boxes in the same row. ...
6
by: Fungii | last post by:
Hello, I have a stylesheet that sets p:first-letter to a certain size and colour. I was playing around with Javascript to change paragraph stylesheets using an array like this: var paras =...
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...
22
by: Luke | last post by:
Elements with name attribute: form, input, textarea, a, frame, iframe, button, select, map, meta, applet, object, param, img (if you know more reply...) Methods of addresing html elements:...
7
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...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
2
by: justplain.kzn | last post by:
Hi, I have a table with dynamic html that contains drop down select lists and readonly text boxes. Dynamic calculations are done on change of a value in one of the drop down select lists. ...
5
by: Neil | last post by:
"lyle" <lyle.fairfield@gmail.comwrote in message news:48c3dde7-07bd-48b8-91c3-e157b703f92b@f3g2000hsg.googlegroups.com... Question for you. I'm doing something similar, only, instead of opening...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
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...

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.