Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 5th, 2003, 05:05 PM
Registered User
Join Date: Dec 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Populating Fields on a Form with Cookie Name-Value

I have a multi-page form.

I have in my cookie all the name-value pairs for every field of every form page & I know how to access those values.

When my user goes back to a page already filled in, I would like to populate the actual form *fields* on that page with the values the user filled in on his/her last visit to the page in that session.

In my "onload()" function, I tried:
(where strNames[i] is the name of that field on the page & strValues[i] is the value of that field on the page)

   for (i = 0; i < strNames.length; i++){
      strNames[i].value = strValues[i]; // set value on page
      alert("strNames[i].value = " + strNames[i].value)
   } //

but strNames[i].value was undefined.

How do I populate the actual form fields (as opposed to popping up alert boxes or writing to the bottom of the page with document.write)?
Reply With Quote
  #2 (permalink)  
Old December 5th, 2003, 05:15 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

You need to figure out the field object based on the name...

     document.forms[0][strNames[i]].value = strValues[i]; // set value on page

I can't say this will definately work (not sure about the element array, it might be form[0].elements[...]), but this is the general idea. You need to use the value from your names array to find the element in the form.

Alternatively you could use the eval() method to find it:

     objFormElement = eval("document.forms[0]."+strNames[i]);
     objFormElement.value = strValues[i]; // set value on page

Lastly, there's the getElementById method, but this work only if you have a value in the ID attribute of each input (I think):

     objFormElement = document.getElementById(strNames[i]);
     objFormElement.value = strValues[i]; // set value on page

(Sorry for the uncertainty of these suggestions. I gave you three in the hopes one would work.)

Work smarter, not harder.
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Populating an unbound form from a table Luis Access VBA 8 November 21st, 2008 08:37 AM
Populating Fields from a textbox teddyk ASP.NET 2.0 Basics 1 August 8th, 2008 06:25 AM
Auto Populating Fields in Form E3-PSU Access VBA 3 August 29th, 2007 04:43 AM
populating online text fields etc hamilton C# 2 June 20th, 2007 09:16 AM
Populating a form maneor Access 1 September 30th, 2004 03:41 PM

All times are GMT -4. The time now is 09:15 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.