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

Help: Problem with accessing form data using javascript.

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[i].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
Jul 23 '05 #1
7 1728
"Ant" <as**@asdf.com> wrote in message
news:ct**********@wisteria.csv.warwick.ac.uk...
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[i].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++
Jul 23 '05 #2
On Tue, 25 Jan 2005 10:01:36 -0600, McKirahan <Ne**@McKirahan.com> wrote:
"Ant" <as**@asdf.com> wrote in message
news:ct**********@wisteria.csv.warwick.ac.uk...
I'm, having some problems with this function.
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[i].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[i].value + '\n';
}
}
<textarea id="output" rows="10" cols="40"></textarea>

[snip]
i++


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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:zt********************@comcast.com...
"Ant" <as**@asdf.com> wrote in message
news:ct**********@wisteria.csv.warwick.ac.uk...
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[i].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[i].value;
} catch (err) { }
}
document.write(out);
}
Jul 23 '05 #4
Ant
Thankyou both very much for your replies. They've been a great help!

Cheers
Jul 23 '05 #5
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?

--
Rob
Jul 23 '05 #6
Ant wrote:
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[i].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[i].value;
}
document.write(myVar)
}

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

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #7
"RobG" <rg***@iinet.net.auau> wrote in message
news:41***********************@per-qv1-newsreader-01.iinet.net.au...
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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #8

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

Similar topics

6
by: James Walker | last post by:
Can some one help I get an error of 'checkIndate' is null or not an object can someone please help. I can't work out why Thanks in advance James <form> <td height="24" colspan="7"...
7
by: ???????J | last post by:
Javascript may inquire the push down menu value, can I inquire the description? The following example, the variable($answer) can be get the menu1's value. For example, if I select first data,...
2
by: ASallade | last post by:
Hello, I've scoured my books and the web, but am still daunted, hopefully some of the users in this newsgroup will have advice for my problem. I am not an experienced javascript programmer,...
1
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? ...
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...
55
by: drhowarddrfine | last post by:
I'm working on a web site that could use some control using js but am concerned about what problems I may have with potential users having their js turned off. Has anyone had any serious problems...
8
by: | last post by:
The problem lies here eval("document.TeeForm.amt.value(S+M)"); S and M suppose to add up and the total suppose to appear on the AMT field but it didn't. Any help? ...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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
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.