473,789 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.lengt h; ++i)
{
document.writel n(document.form s[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 1763
"Ant" <as**@asdf.co m> 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.lengt h; ++i)
{
document.writel n(document.form s[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.co m> 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.lengt h; ++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.writel n(document.form s[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.getEle mentById('outpu t');

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******** ************@co mcast.com...
"Ant" <as**@asdf.co m> 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.lengt h; ++i)
{
document.writel n(document.form s[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.le ngth;
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><ti tle>play</title>
</head><body>
<script type="text/javascript">
function firstFunction() {
var buttonType='<in put type="button" value="Proceed" name="contOn" '
+ ' onclick="second Function();">'
document.write( buttonType);
document.close( );
}

function secondFunction( ) {
alert('I am the second function');
}
</script>
<p>Here is some text</p>
<button onclick="firstF unction();">fir stFunction</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.lengt h; ++i)
{
document.writel n(document.form s[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.lengt h; ++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.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #7
"RobG" <rg***@iinet.ne t.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*****@agrico reunited.com>
comp.lang.javas cript 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
1805
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" valign="top"><form name="booknow"><select
7
1914
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, the menu1's value would be 1. Therefore ($answer) would be 1. But If want using javascript get the description(ABC),which javascript command can do it?
2
1537
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, but have gotten my code to work well, without errors on IE, Opera and netscapes recent builds. While testing, I found that it doesnt execute on Netscape 4.7 In the head I have a script that creates a custom object/class for products,
1
1529
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
6
2746
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 called "form1", and I have selects called "PORTA", "PORTB" ... etc...
55
4216
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 with this sort of thing? I know some of these potential users are with big companies and am wondering if anyone had real problems with that.
8
3716
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? ============================ CODE ==============================================
0
5576
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
53
8420
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 language="javascript" type="text/javascript">
0
9511
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
10404
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...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
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...
0
9016
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6765
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
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
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.