Help: Problem with accessing form data using javascript.

A

Ant

Hi,

I'm, having some problems with this function.

function displayElements()
{
for (i=0;i<document.forms[0].elements.length; ++i)
{
document.writeln(document.forms[0].elements.value);
}
}

I'm trying to loop through the only form (form[0]?) on my webpage and
display all their values. For some reason I'm only being shown the first
value?
I'm not sure what I'm doing wrong, any help much appreciated.
Thanks
 
M

McKirahan

Ant said:
Hi,

I'm, having some problems with this function.

function displayElements()
{
for (i=0;i<document.forms[0].elements.length; ++i)
{
document.writeln(document.forms[0].elements.value);
}
}

I'm trying to loop through the only form (form[0]?) on my webpage and
display all their values. For some reason I'm only being shown the first
value?
I'm not sure what I'm doing wrong, any help much appreciated.
Thanks


i++
 
M

Michael Winter

When posting code, please indent it (preferably using two spaces). It's
much easier to read that way.
function displayElements()
{
for (i=0;i<document.forms[0].elements.length; ++i)

The variable, i, should be declared using the var keyword otherwise it
will become global. It would also be more efficient to save a reference to
the elements collection rather than resolving it twice on every loop
iteration:

var e = document.forms[0].elements;
for(var i = 0, n = e.length; i < n; ++i) {
/* ... */
}
document.writeln(document.forms[0].elements.value);


When you call the write (or writeln) method, the argument is converted to
a string and written to the document stream. However, if the stream is
closed (which happens once the document has finished loading or the
document.close method is called), the stream is re-opened. This causes the
document, and everything in it (including your scripts and forms) to be
deleted. That's why the loop only runs once.

The write (or writeln) method should generally be reserved for dynamically
writing content as a document loads.

For this sort of thing, I would create a TEXTAREA element at the end of
the document, give it an id, and dump your output into that:

function displayElements() {
var e = document.forms[0].elements,
o = document.getElementById('output');

for(var i = 0, n = e.length; i < n; ++i) {
o.value += e.value + '\n';
}
}


<textarea id="output" rows="10" cols="40"></textarea>

[snip]

Why would that help? Both pre- and post-fix increment (and decrement) are
perfectly valid. Out of habit (more than anything else), I always use
prefix variety unless I specifically want the semantics of the latter.

Mike
 
M

McKirahan

McKirahan said:
Ant said:
Hi,

I'm, having some problems with this function.

function displayElements()
{
for (i=0;i<document.forms[0].elements.length; ++i)
{
document.writeln(document.forms[0].elements.value);
}
}

I'm trying to loop through the only form (form[0]?) on my webpage and
display all their values. For some reason I'm only being shown the first
value?
I'm not sure what I'm doing wrong, any help much appreciated.
Thanks


i++


Try this:

function displayElements() {
var frm = document.forms[0];
var max = frm.elements.length;
var out = "";
for (var i=0; i<max; i++) {
try {
out += "<br>" + frm.elements.value;
} catch (err) { }
}
document.write(out);
}
 
R

RobG

Michael Winter wrote:
[...]
When you call the write (or writeln) method, the argument is converted
to a string and written to the document stream. However, if the stream
is closed (which happens once the document has finished loading or the
document.close method is called), the stream is re-opened. This causes
the document, and everything in it (including your scripts and forms)
to be deleted.

Whilst that is what the spec says *should* happen, and it does as far
as I can tell in IE and Firefox (and likely Mozilla and Netscape),
scripts seem to linger in Safari and OmniWeb.

The point being that for some browsers, scripts are not replaced (or at
least, not entirely). I guess this is a bug or at best an
inconsistency with the HTML specification, but developers should not
expect that all contents are removed just by re-opening the document
and writing to it.

For example:

<html><head><title>play</title>
</head><body>
<script type="text/javascript">
function firstFunction(){
var buttonType='<input type="button" value="Proceed" name="contOn" '
+ ' onclick="secondFunction();">'
document.write(buttonType);
document.close();
}

function secondFunction() {
alert('I am the second function');
}
</script>
<p>Here is some text</p>
<button onclick="firstFunction();">firstFunction</button>
<p>Here is some more text</p>
</body</html>

When the first button is clicked, it writes a new button to the page
with an onclick that calls secondFunction. secondFunction should have
been deleted from the document, but it runs. Now this may be because
the script is held in memory (or for some other reason you may guess
at) but the point is it's still "there" in Safari.

Having said that, is there a more explicit way of emptying the
document contents? Perhaps by removing the HTML element?
 
R

Randy Webb

Ant said:
Hi,

I'm, having some problems with this function.

function displayElements()
{
for (i=0;i<document.forms[0].elements.length; ++i)
{
document.writeln(document.forms[0].elements.value);
}
}

I'm trying to loop through the only form (form[0]?) on my webpage and
display all their values. For some reason I'm only being shown the first
value?
I'm not sure what I'm doing wrong, any help much appreciated.


You are using document.write after the page is finished loading. What
that does is totally destroy the current page and replace it. Along with
that page your script is gone. It finds the first element,
document.write's it, and then it's finished because there is no script
left. If you want to print them all out, concatenate a variable and then
print that variable:

function displayElements()
{
var myVar = '';
for (i=0;i<document.forms[0].elements.length; ++i)
{
myVar =+ document.forms[0].elements.value;
}
document.write(myVar)
}

Or consult the group FAQ for DynWrite and dump it to a DIV element in
the page.
 
G

Grant Wagner

RobG said:
Michael Winter wrote:
[...]
When you call the write (or writeln) method, the argument is
converted to a string and written to the document stream. However,
if the stream is closed (which happens once the document has
finished loading or the document.close method is called), the stream
is re-opened. This causes the document, and everything in it
(including your scripts and forms) to be deleted.

Whilst that is what the spec says *should* happen, and it does as far
as I can tell in IE and Firefox (and likely Mozilla and Netscape),
scripts seem to linger in Safari and OmniWeb.

The point being that for some browsers, scripts are not replaced (or
at
least, not entirely). I guess this is a bug or at best an
inconsistency with the HTML specification, but developers should not
expect that all contents are removed just by re-opening the document
and writing to it.

I could not disagree more strongly. Developers should assume without
exception that issuing document.open(), document.write() (which
implicitly calls document.open()), location.href = value, Form#submit()
or any other action that causes the browser to navigate or re-write the
main window content will cause the current document and all script
contained in the current window to be immediately unavailable. Your
example demonstrated behaviour which authors of client-side code should
not even attempt to rely on.

I even regard: <body onunload="alert('Leaving...');"> to be "incorrect"
(although it appears to work in IE 6.0.2900 and Firefox 1.0).

The onunload event fires when the page is unloaded. The code that is
attached to that event is (or in my mind should be) destroyed but the
very event which triggers it.

How would I use onunload then? Well, I wouldn't. But if I did, I'd have
code in another frame, or window, which executes when the event
triggers. Of course, this still leaves the tricky problem that the call
to the code in the other frame or window is wrapped in an anonymous
function in the destroyed window, but I'll overlook that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top